septum 0.1.4 → 0.1.5

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.
Files changed (67) hide show
  1. package/LICENSE +21 -21
  2. package/README.md +105 -105
  3. package/bin/septum.ts +4 -4
  4. package/package.json +63 -63
  5. package/src/cli/commands/check.ts +161 -161
  6. package/src/cli/commands/hook.ts +188 -188
  7. package/src/cli/commands/ingest.ts +29 -29
  8. package/src/cli/commands/init.ts +211 -211
  9. package/src/cli/commands/locate.ts +61 -61
  10. package/src/cli/commands/query.ts +64 -64
  11. package/src/cli/commands/serve.ts +5 -5
  12. package/src/cli/commands/slice.ts +48 -48
  13. package/src/cli/commands/sync.ts +31 -31
  14. package/src/cli/index.ts +210 -210
  15. package/src/cli/templates/hooks.ts +75 -75
  16. package/src/cli/templates/rules.ts +38 -38
  17. package/src/cli/templates/skill.ts +69 -69
  18. package/src/core/boundary/evaluator.ts +232 -210
  19. package/src/core/config/loader.ts +97 -97
  20. package/src/core/config/schema.ts +39 -39
  21. package/src/core/database/client.ts +44 -44
  22. package/src/core/database/repositories/dependency.repository.ts +366 -349
  23. package/src/core/database/repositories/domain.repository.ts +218 -198
  24. package/src/core/database/repositories/file.repository.ts +116 -106
  25. package/src/core/database/repositories/meta.repository.ts +33 -33
  26. package/src/core/database/repositories/symbol.repository.ts +443 -443
  27. package/src/core/database/repository.ts +264 -260
  28. package/src/core/database/schema.ts +153 -153
  29. package/src/core/discovery/topology-detector.ts +486 -486
  30. package/src/core/ingestion/hasher.ts +5 -5
  31. package/src/core/ingestion/pipeline.ts +584 -535
  32. package/src/core/parser/boundary-tracker.ts +288 -288
  33. package/src/core/parser/extractors/base.ts +62 -62
  34. package/src/core/parser/extractors/blade.ts +158 -158
  35. package/src/core/parser/extractors/frontend.ts +194 -194
  36. package/src/core/parser/extractors/go.ts +229 -180
  37. package/src/core/parser/extractors/laravel-semantic.ts +1039 -901
  38. package/src/core/parser/extractors/mcp-cli-semantic.ts +199 -199
  39. package/src/core/parser/extractors/nestjs-semantic.ts +119 -119
  40. package/src/core/parser/extractors/php.ts +153 -153
  41. package/src/core/parser/extractors/python.ts +228 -228
  42. package/src/core/parser/extractors/registry.ts +70 -70
  43. package/src/core/parser/extractors/semantic-extractor.interface.ts +21 -21
  44. package/src/core/parser/extractors/typescript.ts +181 -181
  45. package/src/core/parser/tree-sitter.ts +41 -41
  46. package/src/core/resolver/call-graph-tracer.ts +666 -668
  47. package/src/core/resolver/module-resolver.ts +408 -342
  48. package/src/core/resolver/path-resolver.ts +147 -147
  49. package/src/core/resolver/symbol-locator.ts +543 -543
  50. package/src/core/resolver/vertical-slice-tracer.ts +301 -285
  51. package/src/core/session/session-manager.ts +56 -56
  52. package/src/core/telemetry/telemetry.ts +86 -86
  53. package/src/index.ts +21 -21
  54. package/src/mcp/schemas.ts +578 -286
  55. package/src/mcp/server.ts +875 -716
  56. package/src/mcp/tools/check-boundary.ts +171 -152
  57. package/src/mcp/tools/get-domain-catalog.ts +145 -121
  58. package/src/mcp/tools/get-feature-context.ts +212 -116
  59. package/src/mcp/tools/get-symbol-hotspots.ts +60 -59
  60. package/src/mcp/tools/get-symbol-impact.ts +28 -26
  61. package/src/mcp/tools/get-symbol.ts +65 -63
  62. package/src/mcp/tools/locate-symbol.ts +171 -190
  63. package/src/mcp/tools/register-domain.ts +68 -64
  64. package/src/mcp/tools/register-feature.ts +100 -0
  65. package/src/mcp/tools/trace-vertical-slice.ts +74 -48
  66. package/src/types/index.ts +335 -315
  67. package/src/version.ts +3 -3
@@ -1,199 +1,199 @@
1
- import * as fs from "node:fs";
2
- import * as path from "node:path";
3
- import type { SemanticSliceExtractor, VerticalSliceCandidate } from "./semantic-extractor.interface.ts";
4
-
5
- export class McpCliSemanticExtractor implements SemanticSliceExtractor {
6
- public readonly id = "mcp-cli";
7
- public readonly name = "MCP Server & CLI Semantic Extractor";
8
-
9
- public canHandle(projectRoot: string, framework?: string): boolean {
10
- if (framework === "mcp-server" || framework === "cli") return true;
11
-
12
- // Check presence of CLI or MCP directories
13
- const hasMcp = fs.existsSync(path.join(projectRoot, "src/mcp/tools"));
14
- const hasCli = fs.existsSync(path.join(projectRoot, "src/cli/commands"));
15
- if (hasMcp || hasCli) return true;
16
-
17
- // Check package.json for bin or MCP SDK
18
- const pkgPath = path.join(projectRoot, "package.json");
19
- if (fs.existsSync(pkgPath)) {
20
- try {
21
- const pkg = JSON.parse(fs.readFileSync(pkgPath, "utf-8"));
22
- if (pkg.bin || pkg.dependencies?.["@modelcontextprotocol/sdk"]) {
23
- return true;
24
- }
25
- } catch {}
26
- }
27
-
28
- return false;
29
- }
30
-
31
- public extractSlices(projectRoot: string, domainId?: number): VerticalSliceCandidate[] {
32
- const slices: VerticalSliceCandidate[] = [];
33
-
34
- // 1. Extract MCP Tools
35
- const mcpToolsDir = path.join(projectRoot, "src/mcp/tools");
36
- if (fs.existsSync(mcpToolsDir)) {
37
- try {
38
- const toolFiles = fs
39
- .readdirSync(mcpToolsDir)
40
- .filter((f) => f.endsWith(".ts") || f.endsWith(".js"));
41
-
42
- for (const file of toolFiles) {
43
- const fullPath = path.join(mcpToolsDir, file);
44
- const relPath = path.relative(projectRoot, fullPath);
45
- const content = fs.readFileSync(fullPath, "utf-8");
46
- const baseName = path.basename(file, path.extname(file)); // e.g. 'get-domain-catalog'
47
- const toolIdentifier = `septum_${baseName.replace(/-/g, "_")}`;
48
-
49
- // Detect handler function
50
- const handlerMatch = content.match(/export\s+async?\s+function\s+([a-zA-Z0-9_]+)/);
51
- const actionName = handlerMatch ? handlerMatch[1] : `handle_${baseName}`;
52
-
53
- // Detect args interface
54
- const argsMatch = content.match(/export\s+interface\s+([a-zA-Z0-9_]+Args)/);
55
- const formRequestClass = argsMatch ? argsMatch[1] : null;
56
-
57
- // Detect model/service dependency (e.g. SeptumRepository, ConfigLoader)
58
- let modelClass: string | null = null;
59
- if (content.includes("SeptumRepository")) modelClass = "SeptumRepository";
60
- else if (content.includes("SessionManager")) modelClass = "SessionManager";
61
- else if (content.includes("ConfigLoader")) modelClass = "ConfigLoader";
62
-
63
- const mcpChain: Array<{ stage: string; symbol: string; file?: string; line?: number; description?: string }> = [
64
- {
65
- stage: "ingress",
66
- symbol: `tool:${toolIdentifier}`,
67
- file: relPath,
68
- description: `MCP Tool Endpoint: ${toolIdentifier}`,
69
- },
70
- ];
71
-
72
- if (formRequestClass) {
73
- mcpChain.push({
74
- stage: "validation",
75
- symbol: formRequestClass,
76
- file: relPath,
77
- description: "MCP Argument Schema",
78
- });
79
- }
80
-
81
- mcpChain.push({
82
- stage: "controller",
83
- symbol: `McpToolHandler::${actionName}`,
84
- file: relPath,
85
- line: 1,
86
- description: `Tool Handler Execution`,
87
- });
88
-
89
- if (modelClass) {
90
- mcpChain.push({
91
- stage: "service",
92
- symbol: modelClass,
93
- file: "src/core/database/repository.ts",
94
- description: "Core Engine Service",
95
- });
96
- }
97
-
98
- slices.push({
99
- domain_id: domainId ?? null,
100
- feature_key: `mcp.${baseName}`,
101
- http_method: "MCP",
102
- route_uri: `tool:${toolIdentifier}`,
103
- route_name: toolIdentifier,
104
- controller_class: "McpToolHandler",
105
- action_name: actionName,
106
- controller_file: relPath,
107
- controller_line: 1,
108
- architecture_style: "rpc",
109
- entry_kind: "mcp_tool",
110
- execution_chain_json: JSON.stringify(mcpChain),
111
- });
112
- }
113
- } catch {}
114
- }
115
-
116
- // 2. Extract CLI Commands
117
- const cliCommandsDir = path.join(projectRoot, "src/cli/commands");
118
- if (fs.existsSync(cliCommandsDir)) {
119
- try {
120
- const commandFiles = fs
121
- .readdirSync(cliCommandsDir)
122
- .filter((f) => f.endsWith(".ts") || f.endsWith(".js"));
123
-
124
- for (const file of commandFiles) {
125
- const fullPath = path.join(cliCommandsDir, file);
126
- const relPath = path.relative(projectRoot, fullPath);
127
- const content = fs.readFileSync(fullPath, "utf-8");
128
- const commandName = path.basename(file, path.extname(file)); // e.g. 'ingest', 'check'
129
-
130
- const handlerMatch = content.match(/export\s+async?\s+function\s+([a-zA-Z0-9_]+Command)/);
131
- const actionName = handlerMatch ? handlerMatch[1] : `handle${commandName.charAt(0).toUpperCase() + commandName.slice(1)}Command`;
132
-
133
- // Detect options / args
134
- const optionsMatch = content.match(/interface\s+([a-zA-Z0-9_]+Options)/);
135
- const formRequestClass = optionsMatch ? optionsMatch[1] : null;
136
-
137
- // Detect Core Service used
138
- let modelClass: string | null = null;
139
- if (content.includes("IngestionPipeline")) modelClass = "IngestionPipeline";
140
- else if (content.includes("BoundaryEvaluator")) modelClass = "BoundaryEvaluator";
141
- else if (content.includes("VerticalSliceTracer")) modelClass = "VerticalSliceTracer";
142
- else if (content.includes("SeptumRepository")) modelClass = "SeptumRepository";
143
-
144
- const cliChain: Array<{ stage: string; symbol: string; file?: string; line?: number; description?: string }> = [
145
- {
146
- stage: "ingress",
147
- symbol: `septum ${commandName}`,
148
- file: relPath,
149
- description: `CLI Command Entrypoint: septum ${commandName}`,
150
- },
151
- ];
152
-
153
- if (formRequestClass) {
154
- cliChain.push({
155
- stage: "validation",
156
- symbol: formRequestClass,
157
- file: relPath,
158
- description: "CLI Options Interface",
159
- });
160
- }
161
-
162
- cliChain.push({
163
- stage: "controller",
164
- symbol: `CLICommandHandler::${actionName}`,
165
- file: relPath,
166
- line: 1,
167
- description: "Command Execution Handler",
168
- });
169
-
170
- if (modelClass) {
171
- cliChain.push({
172
- stage: "service",
173
- symbol: modelClass,
174
- file: "src/core/",
175
- description: "Core Engine Service",
176
- });
177
- }
178
-
179
- slices.push({
180
- domain_id: domainId ?? null,
181
- feature_key: `cli.${commandName}`,
182
- http_method: "CLI",
183
- route_uri: `command:${commandName}`,
184
- route_name: `septum ${commandName}`,
185
- controller_class: "CLICommandHandler",
186
- action_name: actionName,
187
- controller_file: relPath,
188
- controller_line: 1,
189
- architecture_style: "cli",
190
- entry_kind: "cli_command",
191
- execution_chain_json: JSON.stringify(cliChain),
192
- });
193
- }
194
- } catch {}
195
- }
196
-
197
- return slices;
198
- }
199
- }
1
+ import * as fs from "node:fs";
2
+ import * as path from "node:path";
3
+ import type { SemanticSliceExtractor, VerticalSliceCandidate } from "./semantic-extractor.interface.ts";
4
+
5
+ export class McpCliSemanticExtractor implements SemanticSliceExtractor {
6
+ public readonly id = "mcp-cli";
7
+ public readonly name = "MCP Server & CLI Semantic Extractor";
8
+
9
+ public canHandle(projectRoot: string, framework?: string): boolean {
10
+ if (framework === "mcp-server" || framework === "cli") return true;
11
+
12
+ // Check presence of CLI or MCP directories
13
+ const hasMcp = fs.existsSync(path.join(projectRoot, "src/mcp/tools"));
14
+ const hasCli = fs.existsSync(path.join(projectRoot, "src/cli/commands"));
15
+ if (hasMcp || hasCli) return true;
16
+
17
+ // Check package.json for bin or MCP SDK
18
+ const pkgPath = path.join(projectRoot, "package.json");
19
+ if (fs.existsSync(pkgPath)) {
20
+ try {
21
+ const pkg = JSON.parse(fs.readFileSync(pkgPath, "utf-8"));
22
+ if (pkg.bin || pkg.dependencies?.["@modelcontextprotocol/sdk"]) {
23
+ return true;
24
+ }
25
+ } catch {}
26
+ }
27
+
28
+ return false;
29
+ }
30
+
31
+ public extractSlices(projectRoot: string, domainId?: number): VerticalSliceCandidate[] {
32
+ const slices: VerticalSliceCandidate[] = [];
33
+
34
+ // 1. Extract MCP Tools
35
+ const mcpToolsDir = path.join(projectRoot, "src/mcp/tools");
36
+ if (fs.existsSync(mcpToolsDir)) {
37
+ try {
38
+ const toolFiles = fs
39
+ .readdirSync(mcpToolsDir)
40
+ .filter((f) => f.endsWith(".ts") || f.endsWith(".js"));
41
+
42
+ for (const file of toolFiles) {
43
+ const fullPath = path.join(mcpToolsDir, file);
44
+ const relPath = path.relative(projectRoot, fullPath);
45
+ const content = fs.readFileSync(fullPath, "utf-8");
46
+ const baseName = path.basename(file, path.extname(file)); // e.g. 'get-domain-catalog'
47
+ const toolIdentifier = `septum_${baseName.replace(/-/g, "_")}`;
48
+
49
+ // Detect handler function
50
+ const handlerMatch = content.match(/export\s+async?\s+function\s+([a-zA-Z0-9_]+)/);
51
+ const actionName = handlerMatch ? handlerMatch[1] : `handle_${baseName}`;
52
+
53
+ // Detect args interface
54
+ const argsMatch = content.match(/export\s+interface\s+([a-zA-Z0-9_]+Args)/);
55
+ const formRequestClass = argsMatch ? argsMatch[1] : null;
56
+
57
+ // Detect model/service dependency (e.g. SeptumRepository, ConfigLoader)
58
+ let modelClass: string | null = null;
59
+ if (content.includes("SeptumRepository")) modelClass = "SeptumRepository";
60
+ else if (content.includes("SessionManager")) modelClass = "SessionManager";
61
+ else if (content.includes("ConfigLoader")) modelClass = "ConfigLoader";
62
+
63
+ const mcpChain: Array<{ stage: string; symbol: string; file?: string; line?: number; description?: string }> = [
64
+ {
65
+ stage: "ingress",
66
+ symbol: `tool:${toolIdentifier}`,
67
+ file: relPath,
68
+ description: `MCP Tool Endpoint: ${toolIdentifier}`,
69
+ },
70
+ ];
71
+
72
+ if (formRequestClass) {
73
+ mcpChain.push({
74
+ stage: "validation",
75
+ symbol: formRequestClass,
76
+ file: relPath,
77
+ description: "MCP Argument Schema",
78
+ });
79
+ }
80
+
81
+ mcpChain.push({
82
+ stage: "controller",
83
+ symbol: `McpToolHandler::${actionName}`,
84
+ file: relPath,
85
+ line: 1,
86
+ description: `Tool Handler Execution`,
87
+ });
88
+
89
+ if (modelClass) {
90
+ mcpChain.push({
91
+ stage: "service",
92
+ symbol: modelClass,
93
+ file: "src/core/database/repository.ts",
94
+ description: "Core Engine Service",
95
+ });
96
+ }
97
+
98
+ slices.push({
99
+ domain_id: domainId ?? null,
100
+ feature_key: `mcp.${baseName}`,
101
+ http_method: "MCP",
102
+ route_uri: `tool:${toolIdentifier}`,
103
+ route_name: toolIdentifier,
104
+ controller_class: "McpToolHandler",
105
+ action_name: actionName,
106
+ controller_file: relPath,
107
+ controller_line: 1,
108
+ architecture_style: "rpc",
109
+ entry_kind: "mcp_tool",
110
+ execution_chain_json: JSON.stringify(mcpChain),
111
+ });
112
+ }
113
+ } catch {}
114
+ }
115
+
116
+ // 2. Extract CLI Commands
117
+ const cliCommandsDir = path.join(projectRoot, "src/cli/commands");
118
+ if (fs.existsSync(cliCommandsDir)) {
119
+ try {
120
+ const commandFiles = fs
121
+ .readdirSync(cliCommandsDir)
122
+ .filter((f) => f.endsWith(".ts") || f.endsWith(".js"));
123
+
124
+ for (const file of commandFiles) {
125
+ const fullPath = path.join(cliCommandsDir, file);
126
+ const relPath = path.relative(projectRoot, fullPath);
127
+ const content = fs.readFileSync(fullPath, "utf-8");
128
+ const commandName = path.basename(file, path.extname(file)); // e.g. 'ingest', 'check'
129
+
130
+ const handlerMatch = content.match(/export\s+async?\s+function\s+([a-zA-Z0-9_]+Command)/);
131
+ const actionName = handlerMatch ? handlerMatch[1] : `handle${commandName.charAt(0).toUpperCase() + commandName.slice(1)}Command`;
132
+
133
+ // Detect options / args
134
+ const optionsMatch = content.match(/interface\s+([a-zA-Z0-9_]+Options)/);
135
+ const formRequestClass = optionsMatch ? optionsMatch[1] : null;
136
+
137
+ // Detect Core Service used
138
+ let modelClass: string | null = null;
139
+ if (content.includes("IngestionPipeline")) modelClass = "IngestionPipeline";
140
+ else if (content.includes("BoundaryEvaluator")) modelClass = "BoundaryEvaluator";
141
+ else if (content.includes("VerticalSliceTracer")) modelClass = "VerticalSliceTracer";
142
+ else if (content.includes("SeptumRepository")) modelClass = "SeptumRepository";
143
+
144
+ const cliChain: Array<{ stage: string; symbol: string; file?: string; line?: number; description?: string }> = [
145
+ {
146
+ stage: "ingress",
147
+ symbol: `septum ${commandName}`,
148
+ file: relPath,
149
+ description: `CLI Command Entrypoint: septum ${commandName}`,
150
+ },
151
+ ];
152
+
153
+ if (formRequestClass) {
154
+ cliChain.push({
155
+ stage: "validation",
156
+ symbol: formRequestClass,
157
+ file: relPath,
158
+ description: "CLI Options Interface",
159
+ });
160
+ }
161
+
162
+ cliChain.push({
163
+ stage: "controller",
164
+ symbol: `CLICommandHandler::${actionName}`,
165
+ file: relPath,
166
+ line: 1,
167
+ description: "Command Execution Handler",
168
+ });
169
+
170
+ if (modelClass) {
171
+ cliChain.push({
172
+ stage: "service",
173
+ symbol: modelClass,
174
+ file: "src/core/",
175
+ description: "Core Engine Service",
176
+ });
177
+ }
178
+
179
+ slices.push({
180
+ domain_id: domainId ?? null,
181
+ feature_key: `cli.${commandName}`,
182
+ http_method: "CLI",
183
+ route_uri: `command:${commandName}`,
184
+ route_name: `septum ${commandName}`,
185
+ controller_class: "CLICommandHandler",
186
+ action_name: actionName,
187
+ controller_file: relPath,
188
+ controller_line: 1,
189
+ architecture_style: "cli",
190
+ entry_kind: "cli_command",
191
+ execution_chain_json: JSON.stringify(cliChain),
192
+ });
193
+ }
194
+ } catch {}
195
+ }
196
+
197
+ return slices;
198
+ }
199
+ }
@@ -1,119 +1,119 @@
1
- import * as fs from "node:fs";
2
- import * as path from "node:path";
3
- import type { SemanticSliceExtractor, VerticalSliceCandidate } from "./semantic-extractor.interface.ts";
4
-
5
- export class NestJsSemanticExtractor implements SemanticSliceExtractor {
6
- public readonly id = "nestjs";
7
- public readonly name = "NestJS Modular Semantic Extractor";
8
-
9
- public canHandle(projectRoot: string, framework?: string): boolean {
10
- if (framework === "nestjs") return true;
11
-
12
- if (fs.existsSync(path.join(projectRoot, "nest-cli.json"))) return true;
13
-
14
- const pkgPath = path.join(projectRoot, "package.json");
15
- if (fs.existsSync(pkgPath)) {
16
- try {
17
- const pkg = JSON.parse(fs.readFileSync(pkgPath, "utf-8"));
18
- const deps = { ...pkg.dependencies, ...pkg.devDependencies };
19
- if (deps["@nestjs/core"]) return true;
20
- } catch {}
21
- }
22
-
23
- return false;
24
- }
25
-
26
- public extractSlices(projectRoot: string, domainId?: number): VerticalSliceCandidate[] {
27
- const slices: VerticalSliceCandidate[] = [];
28
- const srcDir = path.join(projectRoot, "src");
29
- if (!fs.existsSync(srcDir)) return slices;
30
-
31
- const controllerFiles = this.findFilesRecursively(srcDir, /\.controller\.(ts|js)$/);
32
-
33
- for (const file of controllerFiles) {
34
- const relPath = path.relative(projectRoot, file);
35
- const content = fs.readFileSync(file, "utf-8");
36
-
37
- // Extract @Controller('prefix')
38
- const controllerMatch = content.match(/@Controller\s*\(\s*['"]?([^'")]*)['"]?\s*\)\s*export\s+class\s+([A-Za-z0-9_]+)/);
39
- if (!controllerMatch) continue;
40
-
41
- const routePrefix = controllerMatch[1] ? `/${controllerMatch[1].replace(/^\/+|\/+$/g, "")}` : "";
42
- const controllerClass = controllerMatch[2];
43
-
44
- // Extract methods: @Get('path'), @Post('path'), etc.
45
- const methodRegex = /@(Get|Post|Put|Patch|Delete)\s*\(\s*['"]?([^'")]*)['"]?\s*\)\s*(?:@[A-Za-z0-9_]+\s*\([^)]*\)\s*)*\s*(?:async\s+)?([a-zA-Z0-9_]+)\s*\(([^)]*)\)/g;
46
- let match: RegExpExecArray | null;
47
-
48
- while ((match = methodRegex.exec(content)) !== null) {
49
- const httpMethod = match[1].toUpperCase();
50
- const subPath = match[2] ? `/${match[2].replace(/^\/+|\/+$/g, "")}` : "";
51
- const fullUri = `${routePrefix}${subPath}` || "/";
52
- const actionName = match[3];
53
- const params = match[4];
54
-
55
- // Extract DTO from parameters: e.g. @Body() createDto: CreateUserDto
56
- const dtoMatch = params.match(/@Body\s*\([^)]*\)\s*[a-zA-Z0-9_]+\s*:\s*([A-Za-z0-9_]+Dto)/);
57
- const formRequestClass = dtoMatch ? dtoMatch[1] : null;
58
-
59
- const chain: Array<{ stage: string; symbol: string; file?: string; line?: number; description?: string }> = [
60
- {
61
- stage: "ingress",
62
- symbol: `${httpMethod} ${fullUri}`,
63
- file: relPath,
64
- description: `NestJS Route: ${httpMethod} ${fullUri}`,
65
- },
66
- ];
67
-
68
- if (formRequestClass) {
69
- chain.push({
70
- stage: "validation",
71
- symbol: formRequestClass,
72
- description: "DTO Validation",
73
- });
74
- }
75
-
76
- chain.push({
77
- stage: "controller",
78
- symbol: `${controllerClass}::${actionName}`,
79
- file: relPath,
80
- line: 1,
81
- description: `NestJS Controller Action`,
82
- });
83
-
84
- slices.push({
85
- domain_id: domainId ?? null,
86
- feature_key: `nest.${controllerClass.toLowerCase().replace("controller", "")}.${actionName}`,
87
- http_method: httpMethod,
88
- route_uri: fullUri,
89
- route_name: `${controllerClass}.${actionName}`,
90
- controller_class: controllerClass,
91
- action_name: actionName,
92
- controller_file: relPath,
93
- controller_line: 1,
94
- architecture_style: "mvc",
95
- entry_kind: "http_route",
96
- execution_chain_json: JSON.stringify(chain),
97
- });
98
- }
99
- }
100
-
101
- return slices;
102
- }
103
-
104
- private findFilesRecursively(dir: string, pattern: RegExp): string[] {
105
- const results: string[] = [];
106
- try {
107
- const entries = fs.readdirSync(dir, { withFileTypes: true });
108
- for (const entry of entries) {
109
- const full = path.join(dir, entry.name);
110
- if (entry.isDirectory() && !entry.name.startsWith(".") && entry.name !== "node_modules") {
111
- results.push(...this.findFilesRecursively(full, pattern));
112
- } else if (entry.isFile() && pattern.test(entry.name)) {
113
- results.push(full);
114
- }
115
- }
116
- } catch {}
117
- return results;
118
- }
119
- }
1
+ import * as fs from "node:fs";
2
+ import * as path from "node:path";
3
+ import type { SemanticSliceExtractor, VerticalSliceCandidate } from "./semantic-extractor.interface.ts";
4
+
5
+ export class NestJsSemanticExtractor implements SemanticSliceExtractor {
6
+ public readonly id = "nestjs";
7
+ public readonly name = "NestJS Modular Semantic Extractor";
8
+
9
+ public canHandle(projectRoot: string, framework?: string): boolean {
10
+ if (framework === "nestjs") return true;
11
+
12
+ if (fs.existsSync(path.join(projectRoot, "nest-cli.json"))) return true;
13
+
14
+ const pkgPath = path.join(projectRoot, "package.json");
15
+ if (fs.existsSync(pkgPath)) {
16
+ try {
17
+ const pkg = JSON.parse(fs.readFileSync(pkgPath, "utf-8"));
18
+ const deps = { ...pkg.dependencies, ...pkg.devDependencies };
19
+ if (deps["@nestjs/core"]) return true;
20
+ } catch {}
21
+ }
22
+
23
+ return false;
24
+ }
25
+
26
+ public extractSlices(projectRoot: string, domainId?: number): VerticalSliceCandidate[] {
27
+ const slices: VerticalSliceCandidate[] = [];
28
+ const srcDir = path.join(projectRoot, "src");
29
+ if (!fs.existsSync(srcDir)) return slices;
30
+
31
+ const controllerFiles = this.findFilesRecursively(srcDir, /\.controller\.(ts|js)$/);
32
+
33
+ for (const file of controllerFiles) {
34
+ const relPath = path.relative(projectRoot, file);
35
+ const content = fs.readFileSync(file, "utf-8");
36
+
37
+ // Extract @Controller('prefix')
38
+ const controllerMatch = content.match(/@Controller\s*\(\s*['"]?([^'")]*)['"]?\s*\)\s*export\s+class\s+([A-Za-z0-9_]+)/);
39
+ if (!controllerMatch) continue;
40
+
41
+ const routePrefix = controllerMatch[1] ? `/${controllerMatch[1].replace(/^\/+|\/+$/g, "")}` : "";
42
+ const controllerClass = controllerMatch[2];
43
+
44
+ // Extract methods: @Get('path'), @Post('path'), etc.
45
+ const methodRegex = /@(Get|Post|Put|Patch|Delete)\s*\(\s*['"]?([^'")]*)['"]?\s*\)\s*(?:@[A-Za-z0-9_]+\s*\([^)]*\)\s*)*\s*(?:async\s+)?([a-zA-Z0-9_]+)\s*\(([^)]*)\)/g;
46
+ let match: RegExpExecArray | null;
47
+
48
+ while ((match = methodRegex.exec(content)) !== null) {
49
+ const httpMethod = match[1].toUpperCase();
50
+ const subPath = match[2] ? `/${match[2].replace(/^\/+|\/+$/g, "")}` : "";
51
+ const fullUri = `${routePrefix}${subPath}` || "/";
52
+ const actionName = match[3];
53
+ const params = match[4];
54
+
55
+ // Extract DTO from parameters: e.g. @Body() createDto: CreateUserDto
56
+ const dtoMatch = params.match(/@Body\s*\([^)]*\)\s*[a-zA-Z0-9_]+\s*:\s*([A-Za-z0-9_]+Dto)/);
57
+ const formRequestClass = dtoMatch ? dtoMatch[1] : null;
58
+
59
+ const chain: Array<{ stage: string; symbol: string; file?: string; line?: number; description?: string }> = [
60
+ {
61
+ stage: "ingress",
62
+ symbol: `${httpMethod} ${fullUri}`,
63
+ file: relPath,
64
+ description: `NestJS Route: ${httpMethod} ${fullUri}`,
65
+ },
66
+ ];
67
+
68
+ if (formRequestClass) {
69
+ chain.push({
70
+ stage: "validation",
71
+ symbol: formRequestClass,
72
+ description: "DTO Validation",
73
+ });
74
+ }
75
+
76
+ chain.push({
77
+ stage: "controller",
78
+ symbol: `${controllerClass}::${actionName}`,
79
+ file: relPath,
80
+ line: 1,
81
+ description: `NestJS Controller Action`,
82
+ });
83
+
84
+ slices.push({
85
+ domain_id: domainId ?? null,
86
+ feature_key: `nest.${controllerClass.toLowerCase().replace("controller", "")}.${actionName}`,
87
+ http_method: httpMethod,
88
+ route_uri: fullUri,
89
+ route_name: `${controllerClass}.${actionName}`,
90
+ controller_class: controllerClass,
91
+ action_name: actionName,
92
+ controller_file: relPath,
93
+ controller_line: 1,
94
+ architecture_style: "mvc",
95
+ entry_kind: "http_route",
96
+ execution_chain_json: JSON.stringify(chain),
97
+ });
98
+ }
99
+ }
100
+
101
+ return slices;
102
+ }
103
+
104
+ private findFilesRecursively(dir: string, pattern: RegExp): string[] {
105
+ const results: string[] = [];
106
+ try {
107
+ const entries = fs.readdirSync(dir, { withFileTypes: true });
108
+ for (const entry of entries) {
109
+ const full = path.join(dir, entry.name);
110
+ if (entry.isDirectory() && !entry.name.startsWith(".") && entry.name !== "node_modules") {
111
+ results.push(...this.findFilesRecursively(full, pattern));
112
+ } else if (entry.isFile() && pattern.test(entry.name)) {
113
+ results.push(full);
114
+ }
115
+ }
116
+ } catch {}
117
+ return results;
118
+ }
119
+ }