septum 0.1.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 +100 -0
- package/bin/septum.ts +4 -0
- package/package.json +62 -0
- package/src/cli/commands/check.ts +161 -0
- package/src/cli/commands/hook.ts +188 -0
- package/src/cli/commands/ingest.ts +29 -0
- package/src/cli/commands/init.ts +211 -0
- package/src/cli/commands/locate.ts +61 -0
- package/src/cli/commands/query.ts +64 -0
- package/src/cli/commands/serve.ts +5 -0
- package/src/cli/commands/slice.ts +90 -0
- package/src/cli/commands/sync.ts +31 -0
- package/src/cli/index.ts +178 -0
- package/src/cli/templates/hooks.ts +75 -0
- package/src/cli/templates/rules.ts +38 -0
- package/src/cli/templates/skill.ts +66 -0
- package/src/core/boundary/evaluator.ts +210 -0
- package/src/core/config/loader.ts +84 -0
- package/src/core/config/schema.ts +39 -0
- package/src/core/database/client.ts +132 -0
- package/src/core/database/repositories/dependency.repository.ts +279 -0
- package/src/core/database/repositories/domain.repository.ts +198 -0
- package/src/core/database/repositories/file.repository.ts +99 -0
- package/src/core/database/repositories/meta.repository.ts +33 -0
- package/src/core/database/repositories/symbol.repository.ts +424 -0
- package/src/core/database/repository.ts +242 -0
- package/src/core/database/schema.sql +92 -0
- package/src/core/discovery/topology-detector.ts +418 -0
- package/src/core/ingestion/hasher.ts +5 -0
- package/src/core/ingestion/pipeline.ts +438 -0
- package/src/core/parser/boundary-tracker.ts +128 -0
- package/src/core/parser/extractors/base.ts +60 -0
- package/src/core/parser/extractors/frontend.ts +194 -0
- package/src/core/parser/extractors/go.ts +176 -0
- package/src/core/parser/extractors/laravel-semantic.ts +426 -0
- package/src/core/parser/extractors/mcp-cli-semantic.ts +199 -0
- package/src/core/parser/extractors/nestjs-semantic.ts +119 -0
- package/src/core/parser/extractors/php.ts +149 -0
- package/src/core/parser/extractors/python.ts +226 -0
- package/src/core/parser/extractors/registry.ts +49 -0
- package/src/core/parser/extractors/semantic-extractor.interface.ts +21 -0
- package/src/core/parser/extractors/typescript.ts +177 -0
- package/src/core/parser/tree-sitter.ts +39 -0
- package/src/core/resolver/call-graph-tracer.ts +668 -0
- package/src/core/resolver/module-resolver.ts +323 -0
- package/src/core/resolver/symbol-locator.ts +405 -0
- package/src/core/resolver/vertical-slice-tracer.ts +236 -0
- package/src/core/session/session-manager.ts +56 -0
- package/src/core/telemetry/telemetry.ts +86 -0
- package/src/index.ts +18 -0
- package/src/mcp/schemas.ts +68 -0
- package/src/mcp/server.ts +602 -0
- package/src/mcp/tools/check-boundary.ts +102 -0
- package/src/mcp/tools/get-domain-catalog.ts +91 -0
- package/src/mcp/tools/get-feature-context.ts +116 -0
- package/src/mcp/tools/get-symbol-hotspots.ts +49 -0
- package/src/mcp/tools/get-symbol-impact.ts +26 -0
- package/src/mcp/tools/get-symbol.ts +63 -0
- package/src/mcp/tools/locate-symbol.ts +74 -0
- package/src/mcp/tools/register-domain.ts +64 -0
- package/src/mcp/tools/trace-vertical-slice.ts +102 -0
- package/src/types/index.ts +296 -0
|
@@ -0,0 +1,279 @@
|
|
|
1
|
+
import * as fs from "node:fs";
|
|
2
|
+
import type { Database } from "bun:sqlite";
|
|
3
|
+
import type {
|
|
4
|
+
ExtractedDependency,
|
|
5
|
+
GetSymbolImpactResponse,
|
|
6
|
+
InboundCaller,
|
|
7
|
+
VerticalSliceRecord,
|
|
8
|
+
} from "../../../types/index.ts";
|
|
9
|
+
|
|
10
|
+
export class DependencyRepository {
|
|
11
|
+
constructor(private db: Database) {}
|
|
12
|
+
|
|
13
|
+
public replaceFileDependencies(fileId: number, deps: ExtractedDependency[]): void {
|
|
14
|
+
this.db.query("DELETE FROM dependencies WHERE source_file_id = ?").run(fileId);
|
|
15
|
+
if (deps.length === 0) return;
|
|
16
|
+
|
|
17
|
+
const stmt = this.db.query(
|
|
18
|
+
`INSERT INTO dependencies (source_file_id, target_symbol_or_path, import_statement, line_number, is_external)
|
|
19
|
+
VALUES (?, ?, ?, ?, ?)`
|
|
20
|
+
);
|
|
21
|
+
|
|
22
|
+
for (const dep of deps) {
|
|
23
|
+
stmt.run(fileId, dep.target, dep.statement, dep.line_number, dep.is_external ? 1 : 0);
|
|
24
|
+
}
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
public upsertVerticalSlice(
|
|
28
|
+
slice: Omit<VerticalSliceRecord, "id" | "created_at">
|
|
29
|
+
): void {
|
|
30
|
+
const existing = this.db
|
|
31
|
+
.query<VerticalSliceRecord, [string, string]>(
|
|
32
|
+
"SELECT id FROM vertical_slices WHERE http_method = ? AND route_uri = ?"
|
|
33
|
+
)
|
|
34
|
+
.get(slice.http_method, slice.route_uri);
|
|
35
|
+
|
|
36
|
+
if (existing) {
|
|
37
|
+
this.db
|
|
38
|
+
.query(
|
|
39
|
+
`UPDATE vertical_slices
|
|
40
|
+
SET domain_id = ?, feature_key = ?, route_name = ?, controller_class = ?,
|
|
41
|
+
action_name = ?, controller_file = ?, controller_line = ?,
|
|
42
|
+
architecture_style = ?, entry_kind = ?, execution_chain_json = ?
|
|
43
|
+
WHERE id = ?`
|
|
44
|
+
)
|
|
45
|
+
.run(
|
|
46
|
+
slice.domain_id,
|
|
47
|
+
slice.feature_key,
|
|
48
|
+
slice.route_name,
|
|
49
|
+
slice.controller_class,
|
|
50
|
+
slice.action_name,
|
|
51
|
+
slice.controller_file,
|
|
52
|
+
slice.controller_line,
|
|
53
|
+
slice.architecture_style ?? "clean",
|
|
54
|
+
slice.entry_kind ?? "http_route",
|
|
55
|
+
slice.execution_chain_json,
|
|
56
|
+
existing.id
|
|
57
|
+
);
|
|
58
|
+
} else {
|
|
59
|
+
this.db
|
|
60
|
+
.query(
|
|
61
|
+
`INSERT INTO vertical_slices (
|
|
62
|
+
domain_id, feature_key, http_method, route_uri, route_name,
|
|
63
|
+
controller_class, action_name, controller_file, controller_line,
|
|
64
|
+
architecture_style, entry_kind, execution_chain_json
|
|
65
|
+
) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)`
|
|
66
|
+
)
|
|
67
|
+
.run(
|
|
68
|
+
slice.domain_id,
|
|
69
|
+
slice.feature_key,
|
|
70
|
+
slice.http_method,
|
|
71
|
+
slice.route_uri,
|
|
72
|
+
slice.route_name,
|
|
73
|
+
slice.controller_class,
|
|
74
|
+
slice.action_name,
|
|
75
|
+
slice.controller_file,
|
|
76
|
+
slice.controller_line,
|
|
77
|
+
slice.architecture_style ?? "clean",
|
|
78
|
+
slice.entry_kind ?? "http_route",
|
|
79
|
+
slice.execution_chain_json
|
|
80
|
+
);
|
|
81
|
+
}
|
|
82
|
+
}
|
|
83
|
+
|
|
84
|
+
public findVerticalSlices(searchTerm: string): VerticalSliceRecord[] {
|
|
85
|
+
const term = searchTerm.trim();
|
|
86
|
+
const wild = `%${term}%`;
|
|
87
|
+
const query = `
|
|
88
|
+
SELECT * FROM vertical_slices
|
|
89
|
+
WHERE route_uri LIKE ?
|
|
90
|
+
OR route_name LIKE ?
|
|
91
|
+
OR controller_class LIKE ?
|
|
92
|
+
OR action_name LIKE ?
|
|
93
|
+
OR feature_key LIKE ?
|
|
94
|
+
OR execution_chain_json LIKE ?
|
|
95
|
+
ORDER BY id ASC
|
|
96
|
+
`;
|
|
97
|
+
return this.db.query<VerticalSliceRecord, [string, string, string, string, string, string]>(query).all(
|
|
98
|
+
wild, wild, wild, wild, wild, wild
|
|
99
|
+
);
|
|
100
|
+
}
|
|
101
|
+
|
|
102
|
+
public getAllVerticalSlices(): VerticalSliceRecord[] {
|
|
103
|
+
return this.db.query<VerticalSliceRecord, []>("SELECT * FROM vertical_slices ORDER BY id ASC").all();
|
|
104
|
+
}
|
|
105
|
+
|
|
106
|
+
public clearVerticalSlices(domainId?: number): void {
|
|
107
|
+
if (domainId) {
|
|
108
|
+
this.db.query("DELETE FROM vertical_slices WHERE domain_id = ?").run(domainId);
|
|
109
|
+
} else {
|
|
110
|
+
this.db.query("DELETE FROM vertical_slices").run();
|
|
111
|
+
}
|
|
112
|
+
}
|
|
113
|
+
|
|
114
|
+
public getAllDependenciesWithDomains(): Array<{
|
|
115
|
+
source_file: string;
|
|
116
|
+
source_domain: string;
|
|
117
|
+
line_number: number;
|
|
118
|
+
target: string;
|
|
119
|
+
statement: string;
|
|
120
|
+
is_external: boolean;
|
|
121
|
+
}> {
|
|
122
|
+
const query = `
|
|
123
|
+
SELECT
|
|
124
|
+
f.path AS source_file,
|
|
125
|
+
d.name AS source_domain,
|
|
126
|
+
dep.line_number,
|
|
127
|
+
dep.target_symbol_or_path AS target,
|
|
128
|
+
dep.import_statement AS statement,
|
|
129
|
+
dep.is_external
|
|
130
|
+
FROM dependencies dep
|
|
131
|
+
JOIN files f ON dep.source_file_id = f.id
|
|
132
|
+
JOIN domains d ON f.domain_id = d.id
|
|
133
|
+
ORDER BY d.name, f.path, dep.line_number
|
|
134
|
+
`;
|
|
135
|
+
return this.db.query<any, []>(query).all();
|
|
136
|
+
}
|
|
137
|
+
|
|
138
|
+
public getSymbolImpact(symbolQuery: string): GetSymbolImpactResponse {
|
|
139
|
+
const trimmed = symbolQuery.trim();
|
|
140
|
+
if (!trimmed) {
|
|
141
|
+
return {
|
|
142
|
+
target_symbol: symbolQuery,
|
|
143
|
+
impact_summary: {
|
|
144
|
+
total_dependents: 0,
|
|
145
|
+
risk_level: "low",
|
|
146
|
+
affected_files_count: 0,
|
|
147
|
+
},
|
|
148
|
+
inbound_callers: [],
|
|
149
|
+
};
|
|
150
|
+
}
|
|
151
|
+
|
|
152
|
+
let targetContainer = trimmed;
|
|
153
|
+
let targetMethod = "";
|
|
154
|
+
if (trimmed.includes("::")) {
|
|
155
|
+
const parts = trimmed.split("::");
|
|
156
|
+
targetContainer = parts[0];
|
|
157
|
+
targetMethod = parts[1];
|
|
158
|
+
} else if (trimmed.includes("@")) {
|
|
159
|
+
const parts = trimmed.split("@");
|
|
160
|
+
targetContainer = parts[0];
|
|
161
|
+
targetMethod = parts[1];
|
|
162
|
+
}
|
|
163
|
+
|
|
164
|
+
// 1. Inbound dependencies from dependencies table
|
|
165
|
+
const depSql = `
|
|
166
|
+
SELECT
|
|
167
|
+
f.path AS file,
|
|
168
|
+
d.line_number,
|
|
169
|
+
d.target_symbol_or_path,
|
|
170
|
+
dom.name AS domain_name
|
|
171
|
+
FROM dependencies d
|
|
172
|
+
JOIN files f ON d.source_file_id = f.id
|
|
173
|
+
JOIN domains dom ON f.domain_id = dom.id
|
|
174
|
+
WHERE d.target_symbol_or_path LIKE ?
|
|
175
|
+
OR d.target_symbol_or_path LIKE ?
|
|
176
|
+
ORDER BY f.path ASC, d.line_number ASC
|
|
177
|
+
`;
|
|
178
|
+
const depRows = this.db
|
|
179
|
+
.query<any, [string, string]>(depSql)
|
|
180
|
+
.all(`%${targetContainer}%`, `%${trimmed}%`);
|
|
181
|
+
|
|
182
|
+
const callersMap = new Map<string, InboundCaller>();
|
|
183
|
+
|
|
184
|
+
for (const row of depRows) {
|
|
185
|
+
// Find enclosing symbol in caller file if any
|
|
186
|
+
const enclosing = this.db
|
|
187
|
+
.query<{ name: string; kind: string }, [string, number]>(
|
|
188
|
+
`SELECT s.name, s.kind
|
|
189
|
+
FROM symbols s
|
|
190
|
+
JOIN files f ON s.file_id = f.id
|
|
191
|
+
WHERE f.path = ? AND ? BETWEEN s.line_start AND s.line_end
|
|
192
|
+
ORDER BY (s.line_end - s.line_start) ASC LIMIT 1`
|
|
193
|
+
)
|
|
194
|
+
.get(row.file, row.line_number);
|
|
195
|
+
|
|
196
|
+
const callerName = enclosing ? enclosing.name : `${row.file}:${row.line_number}`;
|
|
197
|
+
const key = `${row.file}:${row.line_number}:${callerName}`;
|
|
198
|
+
|
|
199
|
+
if (!callersMap.has(key)) {
|
|
200
|
+
callersMap.set(key, {
|
|
201
|
+
file: row.file,
|
|
202
|
+
caller: callerName,
|
|
203
|
+
line: row.line_number,
|
|
204
|
+
kind: enclosing?.kind || "import",
|
|
205
|
+
domain: row.domain_name,
|
|
206
|
+
});
|
|
207
|
+
}
|
|
208
|
+
}
|
|
209
|
+
|
|
210
|
+
// 2. Scan file content of callers or related files for exact method calls if targetMethod specified
|
|
211
|
+
if (targetMethod) {
|
|
212
|
+
// Look for files importing targetContainer and inspect if they call targetMethod
|
|
213
|
+
const uniqueFiles = Array.from(new Set(depRows.map((r) => r.file)));
|
|
214
|
+
for (const filePath of uniqueFiles) {
|
|
215
|
+
try {
|
|
216
|
+
if (fs.existsSync(filePath)) {
|
|
217
|
+
const content = fs.readFileSync(filePath, "utf-8");
|
|
218
|
+
const lines = content.split(/\r?\n/);
|
|
219
|
+
for (let i = 0; i < lines.length; i++) {
|
|
220
|
+
const line = lines[i];
|
|
221
|
+
if (
|
|
222
|
+
line.includes(`->${targetMethod}(`) ||
|
|
223
|
+
line.includes(`::${targetMethod}(`)
|
|
224
|
+
) {
|
|
225
|
+
const lineNum = i + 1;
|
|
226
|
+
const enclosing = this.db
|
|
227
|
+
.query<{ name: string; kind: string }, [string, number]>(
|
|
228
|
+
`SELECT s.name, s.kind
|
|
229
|
+
FROM symbols s
|
|
230
|
+
JOIN files f ON s.file_id = f.id
|
|
231
|
+
WHERE f.path = ? AND ? BETWEEN s.line_start AND s.line_end
|
|
232
|
+
ORDER BY (s.line_end - s.line_start) ASC LIMIT 1`
|
|
233
|
+
)
|
|
234
|
+
.get(filePath, lineNum);
|
|
235
|
+
|
|
236
|
+
const callerName = enclosing ? enclosing.name : `${filePath}:${lineNum}`;
|
|
237
|
+
const key = `${filePath}:${lineNum}:${callerName}`;
|
|
238
|
+
if (!callersMap.has(key)) {
|
|
239
|
+
callersMap.set(key, {
|
|
240
|
+
file: filePath,
|
|
241
|
+
caller: callerName,
|
|
242
|
+
line: lineNum,
|
|
243
|
+
kind: enclosing?.kind || "call",
|
|
244
|
+
});
|
|
245
|
+
}
|
|
246
|
+
}
|
|
247
|
+
}
|
|
248
|
+
}
|
|
249
|
+
} catch {
|
|
250
|
+
// Ignore read errors
|
|
251
|
+
}
|
|
252
|
+
}
|
|
253
|
+
}
|
|
254
|
+
|
|
255
|
+
const inboundCallers = Array.from(callersMap.values());
|
|
256
|
+
const affectedFiles = new Set(inboundCallers.map((c) => c.file));
|
|
257
|
+
const distinctDomains = new Set(
|
|
258
|
+
inboundCallers.map((c) => c.domain).filter(Boolean)
|
|
259
|
+
);
|
|
260
|
+
|
|
261
|
+
let riskLevel: "low" | "medium" | "high" = "low";
|
|
262
|
+
if (affectedFiles.size > 5 || distinctDomains.size > 2) {
|
|
263
|
+
riskLevel = "high";
|
|
264
|
+
} else if (affectedFiles.size >= 2 || distinctDomains.size >= 2) {
|
|
265
|
+
riskLevel = "medium";
|
|
266
|
+
}
|
|
267
|
+
|
|
268
|
+
return {
|
|
269
|
+
target_symbol: trimmed,
|
|
270
|
+
impact_summary: {
|
|
271
|
+
total_dependents: inboundCallers.length,
|
|
272
|
+
risk_level: riskLevel,
|
|
273
|
+
affected_files_count: affectedFiles.size,
|
|
274
|
+
},
|
|
275
|
+
inbound_callers: inboundCallers,
|
|
276
|
+
};
|
|
277
|
+
}
|
|
278
|
+
|
|
279
|
+
}
|
|
@@ -0,0 +1,198 @@
|
|
|
1
|
+
import type { Database } from "bun:sqlite";
|
|
2
|
+
import type {
|
|
3
|
+
DomainCatalogResponse,
|
|
4
|
+
DomainConfig,
|
|
5
|
+
DomainRecord,
|
|
6
|
+
FileRecord,
|
|
7
|
+
SymbolKind,
|
|
8
|
+
SymbolRecord,
|
|
9
|
+
} from "../../../types/index.ts";
|
|
10
|
+
|
|
11
|
+
export class DomainRepository {
|
|
12
|
+
constructor(private db: Database) {}
|
|
13
|
+
|
|
14
|
+
public upsertDomain(name: string, config: DomainConfig): number {
|
|
15
|
+
const normalizedName = name.trim();
|
|
16
|
+
const existing = this.getDomainByName(normalizedName);
|
|
17
|
+
|
|
18
|
+
// Merge dependencies and archetypes if existing and incoming is empty
|
|
19
|
+
let allowed = config.allowed_dependencies ?? [];
|
|
20
|
+
let forbidden = config.forbidden_dependencies ?? [];
|
|
21
|
+
let archetypes = config.archetypes ?? {};
|
|
22
|
+
|
|
23
|
+
if (existing) {
|
|
24
|
+
if (allowed.length === 0 && existing.allowed_deps_json && existing.allowed_deps_json !== "[]") {
|
|
25
|
+
try {
|
|
26
|
+
allowed = JSON.parse(existing.allowed_deps_json);
|
|
27
|
+
} catch {}
|
|
28
|
+
}
|
|
29
|
+
if (forbidden.length === 0 && existing.forbidden_deps_json && existing.forbidden_deps_json !== "[]") {
|
|
30
|
+
try {
|
|
31
|
+
forbidden = JSON.parse(existing.forbidden_deps_json);
|
|
32
|
+
} catch {}
|
|
33
|
+
}
|
|
34
|
+
if (Object.keys(archetypes).length === 0 && existing.archetypes_json && existing.archetypes_json !== "{}") {
|
|
35
|
+
try {
|
|
36
|
+
archetypes = JSON.parse(existing.archetypes_json);
|
|
37
|
+
} catch {}
|
|
38
|
+
}
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
const allowedJson = JSON.stringify(allowed);
|
|
42
|
+
const forbiddenJson = JSON.stringify(forbidden);
|
|
43
|
+
const archetypesJson = JSON.stringify(archetypes);
|
|
44
|
+
|
|
45
|
+
const query = `
|
|
46
|
+
INSERT INTO domains (name, root_path, allowed_deps_json, forbidden_deps_json, archetypes_json)
|
|
47
|
+
VALUES (?, ?, ?, ?, ?)
|
|
48
|
+
ON CONFLICT(name) DO UPDATE SET
|
|
49
|
+
root_path = excluded.root_path,
|
|
50
|
+
allowed_deps_json = excluded.allowed_deps_json,
|
|
51
|
+
forbidden_deps_json = excluded.forbidden_deps_json,
|
|
52
|
+
archetypes_json = excluded.archetypes_json,
|
|
53
|
+
updated_at = CURRENT_TIMESTAMP
|
|
54
|
+
`;
|
|
55
|
+
this.db.query(query).run(normalizedName, config.root, allowedJson, forbiddenJson, archetypesJson);
|
|
56
|
+
|
|
57
|
+
const record = this.getDomainByName(normalizedName);
|
|
58
|
+
return record?.id ?? 0;
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
public getDomainByName(name: string): DomainRecord | null {
|
|
62
|
+
return this.db
|
|
63
|
+
.query<DomainRecord, [string]>("SELECT * FROM domains WHERE LOWER(name) = LOWER(?)")
|
|
64
|
+
.get(name.trim());
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
public getAllDomains(): DomainRecord[] {
|
|
68
|
+
return this.db.query<DomainRecord, []>("SELECT * FROM domains ORDER BY name ASC").all();
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
public getDomainCatalog(
|
|
72
|
+
domainName: string,
|
|
73
|
+
archetypeFilter?: string
|
|
74
|
+
): DomainCatalogResponse | null {
|
|
75
|
+
const domain = this.getDomainByName(domainName);
|
|
76
|
+
if (!domain) return null;
|
|
77
|
+
|
|
78
|
+
const allowedDeps: string[] = JSON.parse(domain.allowed_deps_json || "[]");
|
|
79
|
+
const forbiddenDeps: string[] = JSON.parse(domain.forbidden_deps_json || "[]");
|
|
80
|
+
|
|
81
|
+
let filesQuery = "SELECT * FROM files WHERE domain_id = ?";
|
|
82
|
+
const params: [number, string?] = [domain.id];
|
|
83
|
+
|
|
84
|
+
if (archetypeFilter) {
|
|
85
|
+
filesQuery += " AND archetype = ?";
|
|
86
|
+
params.push(archetypeFilter);
|
|
87
|
+
}
|
|
88
|
+
filesQuery += " ORDER BY path ASC";
|
|
89
|
+
|
|
90
|
+
const files = this.db.query<FileRecord, any>(filesQuery).all(...params);
|
|
91
|
+
|
|
92
|
+
const catalogFiles: DomainCatalogResponse["files"] = [];
|
|
93
|
+
|
|
94
|
+
for (const file of files) {
|
|
95
|
+
const symbols = this.db
|
|
96
|
+
.query<SymbolRecord, [number]>(
|
|
97
|
+
"SELECT * FROM symbols WHERE file_id = ? ORDER BY line_start ASC"
|
|
98
|
+
)
|
|
99
|
+
.all(file.id);
|
|
100
|
+
|
|
101
|
+
const symbolMap = new Map<
|
|
102
|
+
string,
|
|
103
|
+
{
|
|
104
|
+
name: string;
|
|
105
|
+
kind: SymbolKind;
|
|
106
|
+
properties: string[];
|
|
107
|
+
methods: string[];
|
|
108
|
+
signatures: string[];
|
|
109
|
+
}
|
|
110
|
+
>();
|
|
111
|
+
|
|
112
|
+
for (const sym of symbols) {
|
|
113
|
+
if (
|
|
114
|
+
sym.kind === "class" ||
|
|
115
|
+
sym.kind === "interface" ||
|
|
116
|
+
sym.kind === "trait" ||
|
|
117
|
+
sym.kind === "enum"
|
|
118
|
+
) {
|
|
119
|
+
if (!symbolMap.has(sym.name)) {
|
|
120
|
+
symbolMap.set(sym.name, {
|
|
121
|
+
name: sym.name,
|
|
122
|
+
kind: sym.kind,
|
|
123
|
+
properties: [],
|
|
124
|
+
methods: [],
|
|
125
|
+
signatures: [sym.signature],
|
|
126
|
+
});
|
|
127
|
+
}
|
|
128
|
+
} else if (sym.kind === "method") {
|
|
129
|
+
let targetContainer = Array.from(symbolMap.values()).pop();
|
|
130
|
+
if (sym.name.includes("::")) {
|
|
131
|
+
const containerName = sym.name.split("::")[0];
|
|
132
|
+
if (symbolMap.has(containerName)) {
|
|
133
|
+
targetContainer = symbolMap.get(containerName);
|
|
134
|
+
}
|
|
135
|
+
}
|
|
136
|
+
if (targetContainer) {
|
|
137
|
+
targetContainer.methods.push(sym.signature);
|
|
138
|
+
} else {
|
|
139
|
+
symbolMap.set(sym.name, {
|
|
140
|
+
name: sym.name,
|
|
141
|
+
kind: sym.kind,
|
|
142
|
+
properties: [],
|
|
143
|
+
methods: [],
|
|
144
|
+
signatures: [sym.signature],
|
|
145
|
+
});
|
|
146
|
+
}
|
|
147
|
+
} else if (sym.kind === "property") {
|
|
148
|
+
let targetContainer = Array.from(symbolMap.values()).pop();
|
|
149
|
+
if (sym.name.includes("::")) {
|
|
150
|
+
const containerName = sym.name.split("::")[0];
|
|
151
|
+
if (symbolMap.has(containerName)) {
|
|
152
|
+
targetContainer = symbolMap.get(containerName);
|
|
153
|
+
}
|
|
154
|
+
}
|
|
155
|
+
if (targetContainer) {
|
|
156
|
+
targetContainer.properties.push(sym.signature);
|
|
157
|
+
} else {
|
|
158
|
+
symbolMap.set(sym.name, {
|
|
159
|
+
name: sym.name,
|
|
160
|
+
kind: sym.kind,
|
|
161
|
+
properties: [sym.signature],
|
|
162
|
+
methods: [],
|
|
163
|
+
signatures: [sym.signature],
|
|
164
|
+
});
|
|
165
|
+
}
|
|
166
|
+
} else {
|
|
167
|
+
symbolMap.set(sym.name, {
|
|
168
|
+
name: sym.name,
|
|
169
|
+
kind: sym.kind,
|
|
170
|
+
properties: [],
|
|
171
|
+
methods: [],
|
|
172
|
+
signatures: [sym.signature],
|
|
173
|
+
});
|
|
174
|
+
}
|
|
175
|
+
}
|
|
176
|
+
|
|
177
|
+
catalogFiles.push({
|
|
178
|
+
path: file.path,
|
|
179
|
+
archetype: file.archetype,
|
|
180
|
+
symbols: Array.from(symbolMap.values()).map((s) => ({
|
|
181
|
+
name: s.name,
|
|
182
|
+
kind: s.kind,
|
|
183
|
+
properties: s.properties.length > 0 ? s.properties : undefined,
|
|
184
|
+
methods: s.methods.length > 0 ? s.methods : undefined,
|
|
185
|
+
signatures: s.signatures.length > 0 ? s.signatures : undefined,
|
|
186
|
+
})),
|
|
187
|
+
});
|
|
188
|
+
}
|
|
189
|
+
|
|
190
|
+
return {
|
|
191
|
+
domain: domain.name,
|
|
192
|
+
root: domain.root_path,
|
|
193
|
+
allowed_dependencies: allowedDeps,
|
|
194
|
+
forbidden_dependencies: forbiddenDeps,
|
|
195
|
+
files: catalogFiles,
|
|
196
|
+
};
|
|
197
|
+
}
|
|
198
|
+
}
|
|
@@ -0,0 +1,99 @@
|
|
|
1
|
+
import type { Database } from "bun:sqlite";
|
|
2
|
+
import type { ArchetypeKind, DomainRecord, FileRecord } from "../../../types/index.ts";
|
|
3
|
+
|
|
4
|
+
export class FileRepository {
|
|
5
|
+
constructor(private db: Database) {}
|
|
6
|
+
|
|
7
|
+
public getFileByPath(path: string): FileRecord | null {
|
|
8
|
+
return this.db
|
|
9
|
+
.query<FileRecord, [string]>("SELECT * FROM files WHERE path = ?")
|
|
10
|
+
.get(path);
|
|
11
|
+
}
|
|
12
|
+
|
|
13
|
+
public getFileById(id: number): FileRecord | null {
|
|
14
|
+
return this.db
|
|
15
|
+
.query<FileRecord, [number]>("SELECT * FROM files WHERE id = ?")
|
|
16
|
+
.get(id);
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
public getFilesByDomain(domainId: number): FileRecord[] {
|
|
20
|
+
return this.db
|
|
21
|
+
.query<FileRecord, [number]>("SELECT * FROM files WHERE domain_id = ? ORDER BY path ASC")
|
|
22
|
+
.all(domainId);
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
public upsertFile(
|
|
26
|
+
domainId: number,
|
|
27
|
+
path: string,
|
|
28
|
+
archetype: ArchetypeKind,
|
|
29
|
+
contentHash: string,
|
|
30
|
+
lineCount: number,
|
|
31
|
+
mtimeMs: number = 0,
|
|
32
|
+
sizeBytes: number = 0
|
|
33
|
+
): number {
|
|
34
|
+
const existing = this.getFileByPath(path);
|
|
35
|
+
if (existing) {
|
|
36
|
+
this.db
|
|
37
|
+
.query(
|
|
38
|
+
`UPDATE files
|
|
39
|
+
SET domain_id = ?, archetype = ?, content_hash = ?, line_count = ?, mtime_ms = ?, size_bytes = ?, last_scanned_at = CURRENT_TIMESTAMP
|
|
40
|
+
WHERE id = ?`
|
|
41
|
+
)
|
|
42
|
+
.run(domainId, archetype, contentHash, lineCount, mtimeMs, sizeBytes, existing.id);
|
|
43
|
+
return existing.id;
|
|
44
|
+
} else {
|
|
45
|
+
const result = this.db
|
|
46
|
+
.query(
|
|
47
|
+
`INSERT INTO files (domain_id, path, archetype, content_hash, line_count, mtime_ms, size_bytes)
|
|
48
|
+
VALUES (?, ?, ?, ?, ?, ?, ?)`
|
|
49
|
+
)
|
|
50
|
+
.run(domainId, path, archetype, contentHash, lineCount, mtimeMs, sizeBytes);
|
|
51
|
+
return Number(result.lastInsertRowid);
|
|
52
|
+
}
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
public updateFileMetadataOnly(id: number, mtimeMs: number, sizeBytes: number): void {
|
|
56
|
+
this.db
|
|
57
|
+
.query(
|
|
58
|
+
`UPDATE files
|
|
59
|
+
SET mtime_ms = ?, size_bytes = ?, last_scanned_at = CURRENT_TIMESTAMP
|
|
60
|
+
WHERE id = ?`
|
|
61
|
+
)
|
|
62
|
+
.run(mtimeMs, sizeBytes, id);
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
public deleteFilesNotInPaths(domainId: number, validPaths: string[]): number {
|
|
66
|
+
if (validPaths.length === 0) {
|
|
67
|
+
const res = this.db.query("DELETE FROM files WHERE domain_id = ?").run(domainId);
|
|
68
|
+
return Number(res.changes);
|
|
69
|
+
}
|
|
70
|
+
const existingFiles = this.getFilesByDomain(domainId);
|
|
71
|
+
const validSet = new Set(validPaths);
|
|
72
|
+
let deletedCount = 0;
|
|
73
|
+
for (const f of existingFiles) {
|
|
74
|
+
if (!validSet.has(f.path)) {
|
|
75
|
+
this.db.query("DELETE FROM files WHERE id = ?").run(f.id);
|
|
76
|
+
deletedCount++;
|
|
77
|
+
}
|
|
78
|
+
}
|
|
79
|
+
return deletedCount;
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
public deleteFile(path: string): void {
|
|
83
|
+
this.db.query("DELETE FROM files WHERE path = ?").run(path);
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
public getFileWithDomain(
|
|
87
|
+
filePath: string
|
|
88
|
+
): { file: FileRecord; domain: DomainRecord } | null {
|
|
89
|
+
const file = this.getFileByPath(filePath);
|
|
90
|
+
if (!file) return null;
|
|
91
|
+
|
|
92
|
+
const domain = this.db
|
|
93
|
+
.query<DomainRecord, [number]>("SELECT * FROM domains WHERE id = ?")
|
|
94
|
+
.get(file.domain_id);
|
|
95
|
+
if (!domain) return null;
|
|
96
|
+
|
|
97
|
+
return { file, domain };
|
|
98
|
+
}
|
|
99
|
+
}
|
|
@@ -0,0 +1,33 @@
|
|
|
1
|
+
import type { Database } from "bun:sqlite";
|
|
2
|
+
|
|
3
|
+
export class MetaRepository {
|
|
4
|
+
constructor(private db: Database) {}
|
|
5
|
+
|
|
6
|
+
public getMeta(key: string): string | null {
|
|
7
|
+
const row = this.db
|
|
8
|
+
.query<{ value: string }, [string]>("SELECT value FROM repo_meta WHERE key = ?")
|
|
9
|
+
.get(key);
|
|
10
|
+
return row ? row.value : null;
|
|
11
|
+
}
|
|
12
|
+
|
|
13
|
+
public setMeta(key: string, value: string): void {
|
|
14
|
+
this.db
|
|
15
|
+
.query(
|
|
16
|
+
`INSERT INTO repo_meta (key, value, updated_at)
|
|
17
|
+
VALUES (?, ?, CURRENT_TIMESTAMP)
|
|
18
|
+
ON CONFLICT(key) DO UPDATE SET value = excluded.value, updated_at = CURRENT_TIMESTAMP`
|
|
19
|
+
)
|
|
20
|
+
.run(key, value);
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
public getAllMeta(): Record<string, string> {
|
|
24
|
+
const rows = this.db
|
|
25
|
+
.query<{ key: string; value: string }, []>("SELECT key, value FROM repo_meta")
|
|
26
|
+
.all();
|
|
27
|
+
const result: Record<string, string> = {};
|
|
28
|
+
for (const r of rows) {
|
|
29
|
+
result[r.key] = r.value;
|
|
30
|
+
}
|
|
31
|
+
return result;
|
|
32
|
+
}
|
|
33
|
+
}
|