septum 0.1.0 → 0.1.2

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 (65) hide show
  1. package/LICENSE +21 -21
  2. package/README.md +100 -100
  3. package/bin/septum.ts +4 -4
  4. package/package.json +63 -62
  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 +90 -90
  13. package/src/cli/commands/sync.ts +31 -31
  14. package/src/cli/index.ts +199 -178
  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 +66 -66
  18. package/src/core/boundary/evaluator.ts +210 -210
  19. package/src/core/config/loader.ts +84 -84
  20. package/src/core/config/schema.ts +39 -39
  21. package/src/core/database/client.ts +44 -132
  22. package/src/core/database/repositories/dependency.repository.ts +279 -279
  23. package/src/core/database/repositories/domain.repository.ts +198 -198
  24. package/src/core/database/repositories/file.repository.ts +99 -99
  25. package/src/core/database/repositories/meta.repository.ts +33 -33
  26. package/src/core/database/repositories/symbol.repository.ts +424 -424
  27. package/src/core/database/repository.ts +242 -242
  28. package/src/core/database/schema.ts +120 -0
  29. package/src/core/discovery/topology-detector.ts +418 -418
  30. package/src/core/ingestion/hasher.ts +5 -5
  31. package/src/core/ingestion/pipeline.ts +438 -438
  32. package/src/core/parser/boundary-tracker.ts +128 -128
  33. package/src/core/parser/extractors/base.ts +60 -60
  34. package/src/core/parser/extractors/frontend.ts +194 -194
  35. package/src/core/parser/extractors/go.ts +176 -176
  36. package/src/core/parser/extractors/laravel-semantic.ts +426 -426
  37. package/src/core/parser/extractors/mcp-cli-semantic.ts +199 -199
  38. package/src/core/parser/extractors/nestjs-semantic.ts +119 -119
  39. package/src/core/parser/extractors/php.ts +149 -149
  40. package/src/core/parser/extractors/python.ts +226 -226
  41. package/src/core/parser/extractors/registry.ts +49 -49
  42. package/src/core/parser/extractors/semantic-extractor.interface.ts +21 -21
  43. package/src/core/parser/extractors/typescript.ts +177 -177
  44. package/src/core/parser/tree-sitter.ts +39 -39
  45. package/src/core/resolver/call-graph-tracer.ts +668 -668
  46. package/src/core/resolver/module-resolver.ts +342 -323
  47. package/src/core/resolver/symbol-locator.ts +543 -405
  48. package/src/core/resolver/vertical-slice-tracer.ts +237 -236
  49. package/src/core/session/session-manager.ts +56 -56
  50. package/src/core/telemetry/telemetry.ts +86 -86
  51. package/src/index.ts +18 -18
  52. package/src/mcp/schemas.ts +68 -68
  53. package/src/mcp/server.ts +612 -602
  54. package/src/mcp/tools/check-boundary.ts +102 -102
  55. package/src/mcp/tools/get-domain-catalog.ts +91 -91
  56. package/src/mcp/tools/get-feature-context.ts +116 -116
  57. package/src/mcp/tools/get-symbol-hotspots.ts +49 -49
  58. package/src/mcp/tools/get-symbol-impact.ts +26 -26
  59. package/src/mcp/tools/get-symbol.ts +63 -63
  60. package/src/mcp/tools/locate-symbol.ts +74 -74
  61. package/src/mcp/tools/register-domain.ts +64 -64
  62. package/src/mcp/tools/trace-vertical-slice.ts +102 -102
  63. package/src/types/index.ts +296 -296
  64. package/src/version.ts +3 -0
  65. package/src/core/database/schema.sql +0 -92
@@ -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
+ }