@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,611 @@
|
|
|
1
|
+
import { readOptionalSourceFile } from "../../resources/source-files.ts";
|
|
2
|
+
import { isIgnoredDirectory } from "../../resources/ignore.ts";
|
|
3
|
+
import * as fs from "node:fs";
|
|
4
|
+
import * as path from "node:path";
|
|
5
|
+
import type { SemanticSliceExtractor, VerticalSliceCandidate } from "./semantic-extractor.interface.ts";
|
|
6
|
+
|
|
7
|
+
export class GoSemanticExtractor implements SemanticSliceExtractor {
|
|
8
|
+
public readonly id = "golang";
|
|
9
|
+
public readonly name = "Go HTTP Semantic Slice Extractor (Gin, Echo, Chi)";
|
|
10
|
+
|
|
11
|
+
public canHandle(projectRoot: string, framework?: string): boolean {
|
|
12
|
+
if (
|
|
13
|
+
framework === "gin" ||
|
|
14
|
+
framework === "echo" ||
|
|
15
|
+
framework === "chi" ||
|
|
16
|
+
framework === "fiber" ||
|
|
17
|
+
framework === "generic-go" ||
|
|
18
|
+
framework === "go" ||
|
|
19
|
+
framework === "net/http"
|
|
20
|
+
) {
|
|
21
|
+
return true;
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
const goMod = path.join(projectRoot, "go.mod");
|
|
25
|
+
if (fs.existsSync(goMod)) {
|
|
26
|
+
return true;
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
// Check Go source files for HTTP framework or standard library imports
|
|
30
|
+
const goFiles = this.findGoFiles(projectRoot, 3);
|
|
31
|
+
for (const file of goFiles) {
|
|
32
|
+
try {
|
|
33
|
+
const content = fs.readFileSync(file, "utf-8");
|
|
34
|
+
if (
|
|
35
|
+
content.includes("github.com/gin-gonic/gin") ||
|
|
36
|
+
content.includes("github.com/labstack/echo") ||
|
|
37
|
+
content.includes("github.com/go-chi/chi") ||
|
|
38
|
+
content.includes("github.com/gofiber/fiber") ||
|
|
39
|
+
content.includes('"net/http"')
|
|
40
|
+
) {
|
|
41
|
+
return true;
|
|
42
|
+
}
|
|
43
|
+
} catch {}
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
return false;
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
public extractSlices(projectRoot: string, domainId?: number): VerticalSliceCandidate[] {
|
|
50
|
+
const slices: VerticalSliceCandidate[] = [];
|
|
51
|
+
const goFiles = this.findGoFiles(projectRoot);
|
|
52
|
+
|
|
53
|
+
interface HandlerInfo {
|
|
54
|
+
name: string;
|
|
55
|
+
receiver?: string;
|
|
56
|
+
file: string;
|
|
57
|
+
relPath: string;
|
|
58
|
+
line: number;
|
|
59
|
+
boundDto?: string;
|
|
60
|
+
serviceCall?: string;
|
|
61
|
+
repoCall?: string;
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
interface GoStructInfo {
|
|
65
|
+
name: string;
|
|
66
|
+
file: string;
|
|
67
|
+
relPath: string;
|
|
68
|
+
line: number;
|
|
69
|
+
fields: Map<string, string>;
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
interface GoMethodInfo {
|
|
73
|
+
receiver?: string;
|
|
74
|
+
name: string;
|
|
75
|
+
file: string;
|
|
76
|
+
relPath: string;
|
|
77
|
+
line: number;
|
|
78
|
+
body: string;
|
|
79
|
+
params?: string;
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
// 1. Catalog all handler functions & methods across .go files
|
|
83
|
+
const handlers = new Map<string, HandlerInfo>();
|
|
84
|
+
const goStructs = new Map<string, GoStructInfo>();
|
|
85
|
+
const goMethods = new Map<string, GoMethodInfo>();
|
|
86
|
+
|
|
87
|
+
for (const file of goFiles) {
|
|
88
|
+
const content = readOptionalSourceFile(file, "utf-8");
|
|
89
|
+
if (!content) continue;
|
|
90
|
+
|
|
91
|
+
const relPath = path.relative(projectRoot, file).replace(/\\/g, "/");
|
|
92
|
+
|
|
93
|
+
// Match Go functions and methods:
|
|
94
|
+
// func (h *OrderHandler) CreateOrder(c *gin.Context)
|
|
95
|
+
// func (h OrderHandler) CreateOrder(c echo.Context) error
|
|
96
|
+
// func CreateOrder(w http.ResponseWriter, r *http.Request)
|
|
97
|
+
const funcRegex =
|
|
98
|
+
/func\s*(?:\(\s*[a-zA-Z0-9_]+\s+\*?(?:[a-zA-Z0-9_]+\.)?([a-zA-Z0-9_]+)\s*\)\s*)?([a-zA-Z0-9_]+)\s*\(([\s\S]*?)\)(?:[^{]*)\{/g;
|
|
99
|
+
let fMatch: RegExpExecArray | null;
|
|
100
|
+
|
|
101
|
+
while ((fMatch = funcRegex.exec(content)) !== null) {
|
|
102
|
+
const receiver = fMatch[1];
|
|
103
|
+
const fnName = fMatch[2];
|
|
104
|
+
const params = fMatch[3];
|
|
105
|
+
|
|
106
|
+
// Only catalog HTTP handlers (takes *gin.Context, echo.Context, http.ResponseWriter, *fiber.Ctx)
|
|
107
|
+
const isHttpHandler =
|
|
108
|
+
params.includes("gin.Context") ||
|
|
109
|
+
params.includes("echo.Context") ||
|
|
110
|
+
params.includes("http.ResponseWriter") ||
|
|
111
|
+
params.includes("fiber.Ctx") ||
|
|
112
|
+
params.includes("*http.Request");
|
|
113
|
+
|
|
114
|
+
if (!isHttpHandler) continue;
|
|
115
|
+
|
|
116
|
+
const fnLine = content.slice(0, fMatch.index).split("\n").length;
|
|
117
|
+
const bodyStart = fMatch.index + fMatch[0].length;
|
|
118
|
+
const bodyEnd = this.findGoBraceBlockEnd(content, bodyStart - 1);
|
|
119
|
+
const fnBody = content.slice(bodyStart, bodyEnd);
|
|
120
|
+
|
|
121
|
+
// Detect DTO binding: ShouldBindJSON(&req), c.Bind(&req), Decode(&req), c.BodyParser(&req)
|
|
122
|
+
let boundDto: string | undefined;
|
|
123
|
+
const bindMatch = fnBody.match(/(?:ShouldBind(?:JSON|Query|URI|XML|YAML|Header)?|Bind(?:JSON)?|Decode|(?:Body|Params|Query)Parser)\s*\(\s*&([a-zA-Z0-9_]+)\s*\)/);
|
|
124
|
+
if (bindMatch) {
|
|
125
|
+
const varName = bindMatch[1];
|
|
126
|
+
// Look for explicit type declaration:
|
|
127
|
+
// var req CreateOrderRequest
|
|
128
|
+
// var req models.CreateOrderRequest
|
|
129
|
+
// req := CreateOrderRequest{}
|
|
130
|
+
// req := &CreateOrderRequest{}
|
|
131
|
+
// req := new(CreateOrderRequest)
|
|
132
|
+
const typeRegex = new RegExp(
|
|
133
|
+
`(?:var\\s+${varName}\\s+(?:[a-zA-Z0-9_]+\\.)?([A-Z][a-zA-Z0-9_]+)|` +
|
|
134
|
+
`${varName}\\s*:=\\s*&?(?:[a-zA-Z0-9_]+\\.)?([A-Z][a-zA-Z0-9_]+)\\s*\\{|` +
|
|
135
|
+
`${varName}\\s*:=\\s*new\\s*\\((?:[a-zA-Z0-9_]+\\.)?([A-Z][a-zA-Z0-9_]+)\\))`
|
|
136
|
+
);
|
|
137
|
+
const typeMatch = fnBody.match(typeRegex);
|
|
138
|
+
if (typeMatch) {
|
|
139
|
+
boundDto = typeMatch[1] || typeMatch[2] || typeMatch[3];
|
|
140
|
+
}
|
|
141
|
+
// Note: If no type matched, do NOT synthesize a fake name (strict evidence invariant)
|
|
142
|
+
}
|
|
143
|
+
|
|
144
|
+
// Detect service calls: h.orderService.Process(...) or s.service.Create(...)
|
|
145
|
+
let serviceCall: string | undefined;
|
|
146
|
+
const sMatch = fnBody.match(/([a-zA-Z0-9_]+(?:Service|Usecase|UC))\s*\.\s*([A-Z][a-zA-Z0-9_]+)/i);
|
|
147
|
+
if (sMatch) {
|
|
148
|
+
serviceCall = `${sMatch[1]}::${sMatch[2]}`;
|
|
149
|
+
}
|
|
150
|
+
|
|
151
|
+
// Detect repo/db calls: h.repo.Save(...) or db.Create(&order)
|
|
152
|
+
let repoCall: string | undefined;
|
|
153
|
+
const rMatch = fnBody.match(/(?:([a-zA-Z0-9_]+(?:Repo|Repository))\s*\.\s*([A-Z][a-zA-Z0-9_]+)|db\s*\.\s*(?:Create|Save|Find|First|Update|Delete)\s*\(\s*&?([A-Z][a-zA-Z0-9_]+)\s*\))/i);
|
|
154
|
+
if (rMatch) {
|
|
155
|
+
repoCall = rMatch[1] ? `${rMatch[1]}::${rMatch[2]}` : `DB::${rMatch[3]}`;
|
|
156
|
+
}
|
|
157
|
+
|
|
158
|
+
const info: HandlerInfo = {
|
|
159
|
+
name: fnName,
|
|
160
|
+
receiver,
|
|
161
|
+
file,
|
|
162
|
+
relPath,
|
|
163
|
+
line: fnLine,
|
|
164
|
+
boundDto,
|
|
165
|
+
serviceCall,
|
|
166
|
+
repoCall,
|
|
167
|
+
};
|
|
168
|
+
|
|
169
|
+
if (receiver) {
|
|
170
|
+
handlers.set(`${receiver}.${fnName}`, info);
|
|
171
|
+
} else {
|
|
172
|
+
handlers.set(fnName, info);
|
|
173
|
+
}
|
|
174
|
+
}
|
|
175
|
+
|
|
176
|
+
// Catalog struct definitions
|
|
177
|
+
const structRegex = /type\s+([A-Z][a-zA-Z0-9_]+)\s+(?:struct|interface)\s*\{/g;
|
|
178
|
+
let sDeclMatch: RegExpExecArray | null;
|
|
179
|
+
while ((sDeclMatch = structRegex.exec(content)) !== null) {
|
|
180
|
+
const sName = sDeclMatch[1];
|
|
181
|
+
const sLine = content.slice(0, sDeclMatch.index).split("\n").length;
|
|
182
|
+
const sBodyStart = sDeclMatch.index + sDeclMatch[0].length;
|
|
183
|
+
const sBodyEnd = this.findGoBraceBlockEnd(content, sBodyStart - 1);
|
|
184
|
+
const sBody = content.slice(sBodyStart, sBodyEnd);
|
|
185
|
+
|
|
186
|
+
const fields = new Map<string, string>();
|
|
187
|
+
const fieldRegex = /^\s*([a-zA-Z0-9_]+)\s+\*?(?:[a-zA-Z0-9_]+\.)?([A-Z][a-zA-Z0-9_]+)/gm;
|
|
188
|
+
let fldMatch: RegExpExecArray | null;
|
|
189
|
+
while ((fldMatch = fieldRegex.exec(sBody)) !== null) {
|
|
190
|
+
fields.set(fldMatch[1], fldMatch[2]);
|
|
191
|
+
}
|
|
192
|
+
|
|
193
|
+
goStructs.set(sName, { name: sName, file, relPath, line: sLine, fields });
|
|
194
|
+
}
|
|
195
|
+
|
|
196
|
+
// Catalog all methods & functions for call resolution
|
|
197
|
+
const allFuncRegex =
|
|
198
|
+
/func\s*(?:\(\s*[a-zA-Z0-9_]+\s+\*?(?:[a-zA-Z0-9_]+\.)?([a-zA-Z0-9_]+)\s*\)\s*)?([A-Z][a-zA-Z0-9_]+)\s*\(([\s\S]*?)\)(?:[^{]*)\{/g;
|
|
199
|
+
let afMatch: RegExpExecArray | null;
|
|
200
|
+
while ((afMatch = allFuncRegex.exec(content)) !== null) {
|
|
201
|
+
const rcv = afMatch[1];
|
|
202
|
+
const fn = afMatch[2];
|
|
203
|
+
const fLine = content.slice(0, afMatch.index).split("\n").length;
|
|
204
|
+
const bStart = afMatch.index + afMatch[0].length;
|
|
205
|
+
const bEnd = this.findGoBraceBlockEnd(content, bStart - 1);
|
|
206
|
+
const bText = content.slice(bStart, bEnd);
|
|
207
|
+
const params = afMatch[3];
|
|
208
|
+
if (rcv) {
|
|
209
|
+
goMethods.set(`${rcv}.${fn}`, { receiver: rcv, name: fn, file, relPath, line: fLine, body: bText, params });
|
|
210
|
+
} else {
|
|
211
|
+
goMethods.set(fn, { name: fn, file, relPath, line: fLine, body: bText, params });
|
|
212
|
+
}
|
|
213
|
+
}
|
|
214
|
+
}
|
|
215
|
+
|
|
216
|
+
// 2. Extract route registrations per file with lexical block scoping
|
|
217
|
+
for (const file of goFiles) {
|
|
218
|
+
const content = readOptionalSourceFile(file, "utf-8");
|
|
219
|
+
if (!content) continue;
|
|
220
|
+
|
|
221
|
+
const relPath = path.relative(projectRoot, file).replace(/\\/g, "/");
|
|
222
|
+
|
|
223
|
+
// Find router/setup functions and methods to scope groups and variable types
|
|
224
|
+
const setupFuncRegex =
|
|
225
|
+
/func\s*(?:\(\s*([a-zA-Z0-9_]+)\s+\*?(?:[a-zA-Z0-9_]+\.)?([A-Z][a-zA-Z0-9_]+)\s*\)\s*)?([a-zA-Z0-9_]+)\s*\(([\s\S]*?)\)(?:[^{]*)\{/g;
|
|
226
|
+
let funcMatch: RegExpExecArray | null;
|
|
227
|
+
|
|
228
|
+
while ((funcMatch = setupFuncRegex.exec(content)) !== null) {
|
|
229
|
+
const receiverVar = funcMatch[1];
|
|
230
|
+
const receiverType = funcMatch[2];
|
|
231
|
+
const funcParams = funcMatch[4];
|
|
232
|
+
const funcStart = funcMatch.index;
|
|
233
|
+
const bodyStart = funcStart + funcMatch[0].length;
|
|
234
|
+
const bodyEnd = this.findGoBraceBlockEnd(content, bodyStart - 1);
|
|
235
|
+
const funcBody = content.slice(bodyStart, bodyEnd);
|
|
236
|
+
const funcOffset = content.slice(0, bodyStart).split("\n").length - 1;
|
|
237
|
+
|
|
238
|
+
// Scoped variable -> receiver type mapping (e.g. h -> OrderHandler, s -> Server)
|
|
239
|
+
const scopedVarReceivers = new Map<string, string>();
|
|
240
|
+
if (receiverVar && receiverType) {
|
|
241
|
+
scopedVarReceivers.set(receiverVar, receiverType);
|
|
242
|
+
}
|
|
243
|
+
|
|
244
|
+
// Map parameter variables: (h *http.OrderHandler, u *UserHandler)
|
|
245
|
+
const paramRegex = /([a-zA-Z0-9_]+)\s+\*?(?:[a-zA-Z0-9_]+\.)?([A-Z][a-zA-Z0-9_]+)/g;
|
|
246
|
+
let pMatch: RegExpExecArray | null;
|
|
247
|
+
while ((pMatch = paramRegex.exec(funcParams)) !== null) {
|
|
248
|
+
scopedVarReceivers.set(pMatch[1], pMatch[2]);
|
|
249
|
+
}
|
|
250
|
+
|
|
251
|
+
// Map local variable instantiations: h := &OrderHandler{} or var h OrderHandler
|
|
252
|
+
const localInstRegex = /(?:([a-zA-Z0-9_]+)\s*:=\s*(?:&|new\s*\()?(?:[a-zA-Z0-9_]+\.)?([A-Z][a-zA-Z0-9_]+)|var\s+([a-zA-Z0-9_]+)\s+(?:[a-zA-Z0-9_]+\.)?([A-Z][a-zA-Z0-9_]+))/g;
|
|
253
|
+
let lMatch: RegExpExecArray | null;
|
|
254
|
+
while ((lMatch = localInstRegex.exec(funcBody)) !== null) {
|
|
255
|
+
const varName = lMatch[1] || lMatch[3];
|
|
256
|
+
const typeName = lMatch[2] || lMatch[4];
|
|
257
|
+
if (varName && typeName) {
|
|
258
|
+
scopedVarReceivers.set(varName, typeName);
|
|
259
|
+
}
|
|
260
|
+
}
|
|
261
|
+
|
|
262
|
+
// Scoped router groups: groupVar -> accumulatedPrefix
|
|
263
|
+
const scopedGroupPrefixes = new Map<string, string>();
|
|
264
|
+
|
|
265
|
+
// Gin / Echo groups: v1 := r.Group("/api/v1")
|
|
266
|
+
const groupRegex = /([a-zA-Z0-9_]+)\s*:=\s*([a-zA-Z0-9_]+)\.Group\s*\(\s*["']([^"']*)["']/g;
|
|
267
|
+
let gMatch: RegExpExecArray | null;
|
|
268
|
+
while ((gMatch = groupRegex.exec(funcBody)) !== null) {
|
|
269
|
+
const newGroup = gMatch[1];
|
|
270
|
+
const parentGroup = gMatch[2];
|
|
271
|
+
const subPrefix = gMatch[3].replace(/\/+$/, "");
|
|
272
|
+
|
|
273
|
+
const parentPrefix = scopedGroupPrefixes.get(parentGroup) || "";
|
|
274
|
+
const cleanSub = subPrefix.startsWith("/") ? subPrefix : `/${subPrefix}`;
|
|
275
|
+
scopedGroupPrefixes.set(newGroup, `${parentPrefix}${cleanSub}`);
|
|
276
|
+
}
|
|
277
|
+
|
|
278
|
+
// Chi subrouter Route: r.Route("/articles", func(r chi.Router) { ... })
|
|
279
|
+
const chiRouteRegex = /([a-zA-Z0-9_]+)\.Route\s*\(\s*["']([^"']*)["']\s*,\s*func\s*\(\s*([a-zA-Z0-9_]+)\s+chi\.Router\s*\)/g;
|
|
280
|
+
let cMatch: RegExpExecArray | null;
|
|
281
|
+
while ((cMatch = chiRouteRegex.exec(funcBody)) !== null) {
|
|
282
|
+
const parentRouter = cMatch[1];
|
|
283
|
+
const subPrefix = cMatch[2].replace(/\/+$/, "");
|
|
284
|
+
const subRouter = cMatch[3];
|
|
285
|
+
|
|
286
|
+
const parentPrefix = scopedGroupPrefixes.get(parentRouter) || "";
|
|
287
|
+
const cleanSub = subPrefix.startsWith("/") ? subPrefix : `/${subPrefix}`;
|
|
288
|
+
scopedGroupPrefixes.set(subRouter, `${parentPrefix}${cleanSub}`);
|
|
289
|
+
}
|
|
290
|
+
|
|
291
|
+
// 3. Extract route registrations within this function scope
|
|
292
|
+
// Method 1: Gin / Echo / Chi HTTP method calls: r.GET("/ping", pingHandler), v1.POST("/orders", h.CreateOrder)
|
|
293
|
+
const routeRegex =
|
|
294
|
+
/([a-zA-Z0-9_]+)\.(GET|POST|PUT|PATCH|DELETE|OPTIONS|HEAD|Get|Post|Put|Patch|Delete|Options|Head)\s*\(\s*["']([^"']*)["']\s*,\s*(?:[a-zA-Z0-9_.]+\s*,\s*)*(?:func\s*\([^)]*\)|([a-zA-Z0-9_.]+))/g;
|
|
295
|
+
let rMatch: RegExpExecArray | null;
|
|
296
|
+
|
|
297
|
+
while ((rMatch = routeRegex.exec(funcBody)) !== null) {
|
|
298
|
+
const routerVar = rMatch[1];
|
|
299
|
+
const httpMethod = rMatch[2].toUpperCase();
|
|
300
|
+
const rawUri = rMatch[3];
|
|
301
|
+
const handlerTarget = (rMatch[4] && rMatch[4] !== "func") ? rMatch[4] : "closure";
|
|
302
|
+
const routeLine = funcOffset + funcBody.slice(0, rMatch.index).split("\n").length;
|
|
303
|
+
|
|
304
|
+
const groupPrefix = scopedGroupPrefixes.get(routerVar) || "";
|
|
305
|
+
const cleanSub = rawUri ? (rawUri.startsWith("/") ? rawUri : `/${rawUri}`) : "";
|
|
306
|
+
const fullUri = `${groupPrefix}${cleanSub}` || "/";
|
|
307
|
+
|
|
308
|
+
this.pushSlice(
|
|
309
|
+
slices,
|
|
310
|
+
projectRoot,
|
|
311
|
+
relPath,
|
|
312
|
+
routeLine,
|
|
313
|
+
httpMethod,
|
|
314
|
+
fullUri,
|
|
315
|
+
handlerTarget,
|
|
316
|
+
handlers,
|
|
317
|
+
scopedVarReceivers,
|
|
318
|
+
goStructs,
|
|
319
|
+
goMethods,
|
|
320
|
+
domainId
|
|
321
|
+
);
|
|
322
|
+
}
|
|
323
|
+
|
|
324
|
+
// Method 2: Standard net/http HandleFunc and Handle:
|
|
325
|
+
// http.HandleFunc("/ping", pingHandler)
|
|
326
|
+
// mux.HandleFunc("GET /users", h.GetUsers)
|
|
327
|
+
// mux.HandleFunc("POST /users", h.CreateUser)
|
|
328
|
+
// mux.Handle("/orders", h)
|
|
329
|
+
const netHttpRegex =
|
|
330
|
+
/(?:([a-zA-Z0-9_]+|http)\.)?(?:HandleFunc|Handle)\s*\(\s*["']([^"']*)["']\s*,\s*(?:func\s*\([^)]*\)|([a-zA-Z0-9_.]+))/g;
|
|
331
|
+
let nhMatch: RegExpExecArray | null;
|
|
332
|
+
|
|
333
|
+
while ((nhMatch = netHttpRegex.exec(funcBody)) !== null) {
|
|
334
|
+
const pattern = nhMatch[2].trim();
|
|
335
|
+
const handlerTarget = (nhMatch[3] && nhMatch[3] !== "func") ? nhMatch[3] : "closure";
|
|
336
|
+
const routeLine = funcOffset + funcBody.slice(0, nhMatch.index).split("\n").length;
|
|
337
|
+
|
|
338
|
+
// Parse Go 1.22+ pattern with optional method: "GET /path" or just "/path"
|
|
339
|
+
const patternMatch = pattern.match(/^(GET|POST|PUT|PATCH|DELETE|OPTIONS|HEAD)\s+(.*)$/);
|
|
340
|
+
let httpMethod = "ANY";
|
|
341
|
+
let rawUri = pattern;
|
|
342
|
+
|
|
343
|
+
if (patternMatch) {
|
|
344
|
+
httpMethod = patternMatch[1];
|
|
345
|
+
rawUri = patternMatch[2].trim();
|
|
346
|
+
}
|
|
347
|
+
|
|
348
|
+
const cleanUri = rawUri.startsWith("/") ? rawUri : `/${rawUri}`;
|
|
349
|
+
|
|
350
|
+
this.pushSlice(
|
|
351
|
+
slices,
|
|
352
|
+
projectRoot,
|
|
353
|
+
relPath,
|
|
354
|
+
routeLine,
|
|
355
|
+
httpMethod,
|
|
356
|
+
cleanUri,
|
|
357
|
+
handlerTarget,
|
|
358
|
+
handlers,
|
|
359
|
+
scopedVarReceivers,
|
|
360
|
+
goStructs,
|
|
361
|
+
goMethods,
|
|
362
|
+
domainId
|
|
363
|
+
);
|
|
364
|
+
}
|
|
365
|
+
}
|
|
366
|
+
}
|
|
367
|
+
|
|
368
|
+
return slices;
|
|
369
|
+
}
|
|
370
|
+
|
|
371
|
+
private pushSlice(
|
|
372
|
+
slices: VerticalSliceCandidate[],
|
|
373
|
+
projectRoot: string,
|
|
374
|
+
relPath: string,
|
|
375
|
+
routeLine: number,
|
|
376
|
+
httpMethod: string,
|
|
377
|
+
fullUri: string,
|
|
378
|
+
handlerTarget: string,
|
|
379
|
+
handlers: Map<string, any>,
|
|
380
|
+
scopedVarReceivers: Map<string, string>,
|
|
381
|
+
goStructs: Map<string, any>,
|
|
382
|
+
goMethods: Map<string, any>,
|
|
383
|
+
domainId?: number
|
|
384
|
+
): void {
|
|
385
|
+
let handlerInfo: any = undefined;
|
|
386
|
+
let controllerClass = path.basename(relPath, ".go");
|
|
387
|
+
let actionName = handlerTarget;
|
|
388
|
+
|
|
389
|
+
if (handlerTarget.includes(".")) {
|
|
390
|
+
const parts = handlerTarget.split(".");
|
|
391
|
+
const varName = parts[0];
|
|
392
|
+
const memberName = parts[1];
|
|
393
|
+
actionName = memberName;
|
|
394
|
+
|
|
395
|
+
// Look up variable receiver type
|
|
396
|
+
const receiverType = scopedVarReceivers.get(varName);
|
|
397
|
+
if (receiverType) {
|
|
398
|
+
controllerClass = receiverType;
|
|
399
|
+
handlerInfo = handlers.get(`${receiverType}.${memberName}`);
|
|
400
|
+
} else {
|
|
401
|
+
handlerInfo = handlers.get(handlerTarget);
|
|
402
|
+
if (handlerInfo?.receiver) {
|
|
403
|
+
controllerClass = handlerInfo.receiver;
|
|
404
|
+
}
|
|
405
|
+
}
|
|
406
|
+
} else {
|
|
407
|
+
handlerInfo = handlers.get(handlerTarget);
|
|
408
|
+
if (handlerInfo?.receiver) {
|
|
409
|
+
controllerClass = handlerInfo.receiver;
|
|
410
|
+
}
|
|
411
|
+
}
|
|
412
|
+
|
|
413
|
+
const controllerFile = handlerInfo?.relPath || relPath;
|
|
414
|
+
const controllerLine = handlerInfo?.line || routeLine;
|
|
415
|
+
|
|
416
|
+
const chain: Array<{ stage: string; symbol: string; file?: string; line?: number; description?: string }> = [
|
|
417
|
+
{
|
|
418
|
+
stage: "ingress",
|
|
419
|
+
symbol: `${httpMethod} ${fullUri}`,
|
|
420
|
+
file: relPath,
|
|
421
|
+
line: routeLine,
|
|
422
|
+
description: `Go Route: ${httpMethod} ${fullUri}`,
|
|
423
|
+
},
|
|
424
|
+
];
|
|
425
|
+
|
|
426
|
+
if (handlerInfo?.boundDto) {
|
|
427
|
+
chain.push({
|
|
428
|
+
stage: "validation",
|
|
429
|
+
symbol: handlerInfo.boundDto,
|
|
430
|
+
description: `Request DTO Binding: ${handlerInfo.boundDto}`,
|
|
431
|
+
});
|
|
432
|
+
}
|
|
433
|
+
|
|
434
|
+
chain.push({
|
|
435
|
+
stage: "controller",
|
|
436
|
+
symbol: `${controllerClass}::${actionName}`,
|
|
437
|
+
file: controllerFile,
|
|
438
|
+
line: controllerLine,
|
|
439
|
+
description: `Go Route Handler: ${actionName}`,
|
|
440
|
+
});
|
|
441
|
+
|
|
442
|
+
let serviceFile: string | undefined;
|
|
443
|
+
let serviceLine: number | undefined;
|
|
444
|
+
let repoCall = handlerInfo?.repoCall;
|
|
445
|
+
let repoFile: string | undefined;
|
|
446
|
+
let repoLine: number | undefined;
|
|
447
|
+
let entityName: string | undefined;
|
|
448
|
+
let entityFile: string | undefined;
|
|
449
|
+
let entityLine: number | undefined;
|
|
450
|
+
|
|
451
|
+
let targetMethod: any = undefined;
|
|
452
|
+
let targetStruct: any = undefined;
|
|
453
|
+
|
|
454
|
+
if (handlerInfo?.serviceCall) {
|
|
455
|
+
const parts = handlerInfo.serviceCall.split("::");
|
|
456
|
+
const callVar = parts[0];
|
|
457
|
+
const callMethod = parts[1];
|
|
458
|
+
|
|
459
|
+
const handlerStruct = goStructs.get(controllerClass);
|
|
460
|
+
const svcType = handlerStruct?.fields.get(callVar) || callVar;
|
|
461
|
+
targetMethod = goMethods.get(`${svcType}.${callMethod}`) || goMethods.get(`${callVar}.${callMethod}`);
|
|
462
|
+
targetStruct = goStructs.get(svcType);
|
|
463
|
+
|
|
464
|
+
serviceFile = targetMethod?.relPath || targetStruct?.relPath;
|
|
465
|
+
serviceLine = targetMethod?.line || targetStruct?.line;
|
|
466
|
+
|
|
467
|
+
// Scan service body for repo or DB persistence
|
|
468
|
+
if (targetMethod?.body) {
|
|
469
|
+
const rMatch = targetMethod.body.match(
|
|
470
|
+
/(?:(?:[a-zA-Z0-9_]+\.)?([a-zA-Z0-9_]*(?:repo|repository))\s*\.\s*([A-Z][a-zA-Z0-9_]+)|db\s*\.\s*(?:Create|Save|Find|First|Update|Delete)\s*\(\s*&?([A-Z][a-zA-Z0-9_]+)\s*\))/i
|
|
471
|
+
);
|
|
472
|
+
if (rMatch && !repoCall) {
|
|
473
|
+
repoCall = rMatch[1] ? `${rMatch[1]}::${rMatch[2]}` : `DB::${rMatch[3]}`;
|
|
474
|
+
}
|
|
475
|
+
}
|
|
476
|
+
}
|
|
477
|
+
|
|
478
|
+
if (repoCall) {
|
|
479
|
+
const rParts = repoCall.split("::");
|
|
480
|
+
const rVar = rParts[0];
|
|
481
|
+
const rMethod = rParts[1];
|
|
482
|
+
|
|
483
|
+
let rType = rVar;
|
|
484
|
+
const handlerStruct = goStructs.get(controllerClass);
|
|
485
|
+
if (handlerStruct?.fields?.has(rVar)) {
|
|
486
|
+
rType = handlerStruct.fields.get(rVar)!;
|
|
487
|
+
} else if (targetStruct?.fields?.has(rVar)) {
|
|
488
|
+
rType = targetStruct.fields.get(rVar)!;
|
|
489
|
+
}
|
|
490
|
+
rType = rType.replace(/^\*?(?:[a-zA-Z0-9_]+\.)?/, "");
|
|
491
|
+
|
|
492
|
+
const rMethodInfo = goMethods.get(`${rType}.${rMethod}`) || goMethods.get(`${rVar}.${rMethod}`);
|
|
493
|
+
const rStructInfo = goStructs.get(rType);
|
|
494
|
+
repoFile = rMethodInfo?.relPath || rStructInfo?.relPath;
|
|
495
|
+
repoLine = rMethodInfo?.line || rStructInfo?.line;
|
|
496
|
+
|
|
497
|
+
if (rVar === "DB" && rMethod) {
|
|
498
|
+
entityName = rMethod;
|
|
499
|
+
} else if (rMethodInfo?.body) {
|
|
500
|
+
const entMatch = rMethodInfo.body.match(/&?([A-Z][a-zA-Z0-9_]+)\s*\{/);
|
|
501
|
+
if (entMatch) entityName = entMatch[1];
|
|
502
|
+
}
|
|
503
|
+
if (!entityName && rMethodInfo?.params) {
|
|
504
|
+
const pMatch = rMethodInfo.params.match(/\*?(?:[a-zA-Z0-9_]+\.)?([A-Z][a-zA-Z0-9_]+)\b/);
|
|
505
|
+
if (pMatch && goStructs.has(pMatch[1])) entityName = pMatch[1];
|
|
506
|
+
}
|
|
507
|
+
if (!entityName && targetMethod?.params) {
|
|
508
|
+
const pMatch = targetMethod.params.match(/\*?(?:[a-zA-Z0-9_]+\.)?([A-Z][a-zA-Z0-9_]+)\b/);
|
|
509
|
+
if (pMatch && goStructs.has(pMatch[1])) entityName = pMatch[1];
|
|
510
|
+
}
|
|
511
|
+
}
|
|
512
|
+
|
|
513
|
+
if (!entityName && handlerInfo?.boundDto) {
|
|
514
|
+
const candidate = handlerInfo.boundDto.replace(/(?:Request|Req|DTO|Dto)$/, "");
|
|
515
|
+
if (candidate && goStructs.has(candidate)) {
|
|
516
|
+
entityName = candidate;
|
|
517
|
+
}
|
|
518
|
+
}
|
|
519
|
+
|
|
520
|
+
if (entityName && goStructs.has(entityName)) {
|
|
521
|
+
const entInfo = goStructs.get(entityName)!;
|
|
522
|
+
entityFile = entInfo.relPath;
|
|
523
|
+
entityLine = entInfo.line;
|
|
524
|
+
}
|
|
525
|
+
|
|
526
|
+
if (handlerInfo?.serviceCall) {
|
|
527
|
+
chain.push({
|
|
528
|
+
stage: "service",
|
|
529
|
+
symbol: handlerInfo.serviceCall,
|
|
530
|
+
...(serviceFile ? { file: serviceFile, line: serviceLine } : {}),
|
|
531
|
+
description: serviceFile ? "Go Service / Business Logic" : `Service / Business Logic`,
|
|
532
|
+
});
|
|
533
|
+
}
|
|
534
|
+
|
|
535
|
+
if (repoCall) {
|
|
536
|
+
chain.push({
|
|
537
|
+
stage: "repository",
|
|
538
|
+
symbol: repoCall,
|
|
539
|
+
...(repoFile ? { file: repoFile, line: repoLine } : {}),
|
|
540
|
+
description: repoFile ? "Go Repository / Persistence Layer" : `Persistence / Database Operation`,
|
|
541
|
+
});
|
|
542
|
+
}
|
|
543
|
+
|
|
544
|
+
if (entityName) {
|
|
545
|
+
chain.push({
|
|
546
|
+
stage: "entity",
|
|
547
|
+
symbol: entityName,
|
|
548
|
+
...(entityFile ? { file: entityFile, line: entityLine } : {}),
|
|
549
|
+
description: entityFile ? "Go Database / Domain Entity" : `Domain Entity`,
|
|
550
|
+
});
|
|
551
|
+
}
|
|
552
|
+
|
|
553
|
+
slices.push({
|
|
554
|
+
domain_id: domainId ?? null,
|
|
555
|
+
feature_key: null,
|
|
556
|
+
http_method: httpMethod,
|
|
557
|
+
route_uri: fullUri,
|
|
558
|
+
route_name: `${controllerClass}.${actionName}`,
|
|
559
|
+
controller_class: controllerClass,
|
|
560
|
+
action_name: actionName,
|
|
561
|
+
controller_file: controllerFile,
|
|
562
|
+
controller_line: controllerLine,
|
|
563
|
+
architecture_style: "go-http",
|
|
564
|
+
entry_kind: "http_route",
|
|
565
|
+
execution_chain_json: JSON.stringify(chain),
|
|
566
|
+
});
|
|
567
|
+
}
|
|
568
|
+
|
|
569
|
+
private findGoBraceBlockEnd(content: string, startIndex: number): number {
|
|
570
|
+
let depth = 0;
|
|
571
|
+
let started = false;
|
|
572
|
+
|
|
573
|
+
for (let i = startIndex; i < content.length; i++) {
|
|
574
|
+
const char = content[i];
|
|
575
|
+
if (char === "{") {
|
|
576
|
+
depth++;
|
|
577
|
+
started = true;
|
|
578
|
+
} else if (char === "}") {
|
|
579
|
+
depth--;
|
|
580
|
+
if (started && depth === 0) {
|
|
581
|
+
return i;
|
|
582
|
+
}
|
|
583
|
+
}
|
|
584
|
+
}
|
|
585
|
+
|
|
586
|
+
return content.length;
|
|
587
|
+
}
|
|
588
|
+
|
|
589
|
+
private findGoFiles(dir: string, maxDepth: number = 10): string[] {
|
|
590
|
+
const results: string[] = [];
|
|
591
|
+
const traverse = (currentDir: string, currentDepth: number) => {
|
|
592
|
+
if (currentDepth > maxDepth || !fs.existsSync(currentDir)) return;
|
|
593
|
+
try {
|
|
594
|
+
const entries = fs.readdirSync(currentDir, { withFileTypes: true });
|
|
595
|
+
for (const entry of entries) {
|
|
596
|
+
if (isIgnoredDirectory(entry.name)) continue;
|
|
597
|
+
|
|
598
|
+
const fullPath = path.join(currentDir, entry.name);
|
|
599
|
+
if (entry.isDirectory()) {
|
|
600
|
+
traverse(fullPath, currentDepth + 1);
|
|
601
|
+
} else if (entry.isFile() && entry.name.endsWith(".go") && !entry.name.endsWith("_test.go")) {
|
|
602
|
+
results.push(fullPath);
|
|
603
|
+
}
|
|
604
|
+
}
|
|
605
|
+
} catch {}
|
|
606
|
+
};
|
|
607
|
+
|
|
608
|
+
traverse(dir, 0);
|
|
609
|
+
return results;
|
|
610
|
+
}
|
|
611
|
+
}
|