codeflowmap 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 +137 -0
- package/bin/code-mapper.js +961 -0
- package/package.json +47 -0
- package/web/assets/index-2WkfMrUq.js +330 -0
- package/web/assets/index-Dl4QiLwu.css +1 -0
- package/web/graph.json +1273 -0
- package/web/index.html +13 -0
|
@@ -0,0 +1,961 @@
|
|
|
1
|
+
#!/usr/bin/env bun
|
|
2
|
+
// @bun
|
|
3
|
+
|
|
4
|
+
// packages/cli/src/index.ts
|
|
5
|
+
import path8 from "path";
|
|
6
|
+
import { Command } from "commander";
|
|
7
|
+
// packages/core/src/scan.ts
|
|
8
|
+
import fs2 from "fs";
|
|
9
|
+
import path3 from "path";
|
|
10
|
+
|
|
11
|
+
// packages/core/src/util/paths.ts
|
|
12
|
+
import path from "path";
|
|
13
|
+
function assertInside(root, target) {
|
|
14
|
+
const r = path.resolve(root);
|
|
15
|
+
const t = path.resolve(target);
|
|
16
|
+
const rel = path.relative(r, t);
|
|
17
|
+
if (rel === "")
|
|
18
|
+
return t;
|
|
19
|
+
if (rel === ".." || rel.startsWith(".." + path.sep) || path.isAbsolute(rel)) {
|
|
20
|
+
throw new Error(`Path escapes scan root: ${target}`);
|
|
21
|
+
}
|
|
22
|
+
return t;
|
|
23
|
+
}
|
|
24
|
+
function toId(root, file) {
|
|
25
|
+
return path.relative(root, file).split(path.sep).join("/");
|
|
26
|
+
}
|
|
27
|
+
var RESTRICTED = new Set([
|
|
28
|
+
"/etc",
|
|
29
|
+
"/usr",
|
|
30
|
+
"/bin",
|
|
31
|
+
"/sbin",
|
|
32
|
+
"/var",
|
|
33
|
+
"/opt",
|
|
34
|
+
"/System",
|
|
35
|
+
"/Library",
|
|
36
|
+
"/boot",
|
|
37
|
+
"/dev",
|
|
38
|
+
"/proc"
|
|
39
|
+
]);
|
|
40
|
+
function assertSafeOutputDir(dir) {
|
|
41
|
+
const resolved = path.resolve(dir);
|
|
42
|
+
const fsRoot = path.parse(resolved).root;
|
|
43
|
+
if (resolved === fsRoot || RESTRICTED.has(resolved)) {
|
|
44
|
+
throw new Error(`Refusing to write to restricted directory: ${resolved}`);
|
|
45
|
+
}
|
|
46
|
+
const home = process.env.HOME;
|
|
47
|
+
if (home && resolved === path.resolve(home)) {
|
|
48
|
+
throw new Error(`Refusing to write directly into home directory: ${resolved}`);
|
|
49
|
+
}
|
|
50
|
+
return resolved;
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
// packages/core/src/langs/ts-js.ts
|
|
54
|
+
import fs from "fs";
|
|
55
|
+
import path2 from "path";
|
|
56
|
+
import { Node, Project, SyntaxKind } from "ts-morph";
|
|
57
|
+
function langOf(file) {
|
|
58
|
+
const ext = path2.extname(file);
|
|
59
|
+
return ext === ".ts" || ext === ".tsx" ? "typescript" : "javascript";
|
|
60
|
+
}
|
|
61
|
+
function extractTsJs(root, files) {
|
|
62
|
+
const tsconfig = path2.join(root, "tsconfig.json");
|
|
63
|
+
const project = fs.existsSync(tsconfig) ? new Project({ tsConfigFilePath: tsconfig, skipAddingFilesFromTsConfig: true }) : new Project({ compilerOptions: { allowJs: true } });
|
|
64
|
+
for (const f of files)
|
|
65
|
+
project.addSourceFileAtPathIfExists(f);
|
|
66
|
+
const known = new Set(files.map((f) => path2.resolve(f)));
|
|
67
|
+
const nodes = [];
|
|
68
|
+
const edges = [];
|
|
69
|
+
const calls = [];
|
|
70
|
+
for (const sf of project.getSourceFiles()) {
|
|
71
|
+
const fp = sf.getFilePath();
|
|
72
|
+
if (!known.has(path2.resolve(fp)))
|
|
73
|
+
continue;
|
|
74
|
+
const id = toId(root, fp);
|
|
75
|
+
const symbols = [];
|
|
76
|
+
try {
|
|
77
|
+
for (const [name, decls] of sf.getExportedDeclarations()) {
|
|
78
|
+
symbols.push({ name, kind: decls[0]?.getKindName() ?? "unknown" });
|
|
79
|
+
}
|
|
80
|
+
} catch {}
|
|
81
|
+
nodes.push({ id, path: id, language: langOf(fp), symbols });
|
|
82
|
+
const importDecls = [...sf.getImportDeclarations(), ...sf.getExportDeclarations()];
|
|
83
|
+
const seenImports = new Set;
|
|
84
|
+
for (const d of importDecls) {
|
|
85
|
+
let target;
|
|
86
|
+
try {
|
|
87
|
+
target = d.getModuleSpecifierSourceFile();
|
|
88
|
+
} catch {
|
|
89
|
+
continue;
|
|
90
|
+
}
|
|
91
|
+
if (!target)
|
|
92
|
+
continue;
|
|
93
|
+
const tp = path2.resolve(target.getFilePath());
|
|
94
|
+
if (!known.has(tp))
|
|
95
|
+
continue;
|
|
96
|
+
const to = toId(root, target.getFilePath());
|
|
97
|
+
if (to === id || seenImports.has(to))
|
|
98
|
+
continue;
|
|
99
|
+
seenImports.add(to);
|
|
100
|
+
edges.push({ from: id, to, kind: "import" });
|
|
101
|
+
}
|
|
102
|
+
const seenCalls = new Set;
|
|
103
|
+
for (const call of sf.getDescendantsOfKind(SyntaxKind.CallExpression)) {
|
|
104
|
+
const target = resolveCallTarget(call, root, known);
|
|
105
|
+
if (!target)
|
|
106
|
+
continue;
|
|
107
|
+
const fromSymbol = enclosingName(call);
|
|
108
|
+
const key = `${fromSymbol}\x00${target.to}\x00${target.toSymbol}`;
|
|
109
|
+
if (seenCalls.has(key))
|
|
110
|
+
continue;
|
|
111
|
+
seenCalls.add(key);
|
|
112
|
+
calls.push({ from: id, fromSymbol, to: target.to, toSymbol: target.toSymbol });
|
|
113
|
+
}
|
|
114
|
+
}
|
|
115
|
+
return { nodes, edges, calls };
|
|
116
|
+
}
|
|
117
|
+
function resolveCallTarget(call, root, known) {
|
|
118
|
+
const expr = call.getExpression();
|
|
119
|
+
try {
|
|
120
|
+
let sym = expr.getSymbol();
|
|
121
|
+
if (!sym)
|
|
122
|
+
return null;
|
|
123
|
+
const aliased = sym.getAliasedSymbol();
|
|
124
|
+
if (aliased)
|
|
125
|
+
sym = aliased;
|
|
126
|
+
const decl = sym.getDeclarations()[0];
|
|
127
|
+
if (!decl)
|
|
128
|
+
return null;
|
|
129
|
+
if (Node.isParameterDeclaration(decl) || Node.isBindingElement(decl))
|
|
130
|
+
return null;
|
|
131
|
+
const declFile = path2.resolve(decl.getSourceFile().getFilePath());
|
|
132
|
+
if (!known.has(declFile))
|
|
133
|
+
return null;
|
|
134
|
+
const name = sym.getName();
|
|
135
|
+
if (!name || name === "default")
|
|
136
|
+
return null;
|
|
137
|
+
return { to: toId(root, decl.getSourceFile().getFilePath()), toSymbol: name };
|
|
138
|
+
} catch {
|
|
139
|
+
return null;
|
|
140
|
+
}
|
|
141
|
+
}
|
|
142
|
+
function enclosingName(node) {
|
|
143
|
+
let cur = node.getParent();
|
|
144
|
+
while (cur) {
|
|
145
|
+
if (Node.isFunctionDeclaration(cur) || Node.isMethodDeclaration(cur)) {
|
|
146
|
+
const n = cur.getName();
|
|
147
|
+
if (n)
|
|
148
|
+
return n;
|
|
149
|
+
}
|
|
150
|
+
if (Node.isVariableDeclaration(cur)) {
|
|
151
|
+
const init = cur.getInitializer();
|
|
152
|
+
if (init && (Node.isArrowFunction(init) || Node.isFunctionExpression(init))) {
|
|
153
|
+
return cur.getName();
|
|
154
|
+
}
|
|
155
|
+
}
|
|
156
|
+
cur = cur.getParent();
|
|
157
|
+
}
|
|
158
|
+
return "<module>";
|
|
159
|
+
}
|
|
160
|
+
|
|
161
|
+
// packages/core/src/langs/python.ts
|
|
162
|
+
import { createRequire } from "module";
|
|
163
|
+
import { Language, Parser } from "web-tree-sitter";
|
|
164
|
+
var require2 = createRequire(import.meta.url);
|
|
165
|
+
var cached = null;
|
|
166
|
+
function createPythonExtractor() {
|
|
167
|
+
if (!cached)
|
|
168
|
+
cached = init();
|
|
169
|
+
return cached;
|
|
170
|
+
}
|
|
171
|
+
async function init() {
|
|
172
|
+
const coreWasm = require2.resolve("web-tree-sitter/web-tree-sitter.wasm");
|
|
173
|
+
await Parser.init({ locateFile: () => coreWasm });
|
|
174
|
+
const grammar = require2.resolve("tree-sitter-python/tree-sitter-python.wasm");
|
|
175
|
+
const python = await Language.load(grammar);
|
|
176
|
+
const parser = new Parser;
|
|
177
|
+
parser.setLanguage(python);
|
|
178
|
+
return {
|
|
179
|
+
extract(fileId, content, pyIds) {
|
|
180
|
+
const symbols = [];
|
|
181
|
+
const edges = [];
|
|
182
|
+
const seen = new Set;
|
|
183
|
+
const tree = parser.parse(content);
|
|
184
|
+
if (tree)
|
|
185
|
+
walk(tree.rootNode, fileId, pyIds, symbols, edges, seen);
|
|
186
|
+
return { symbols, edges };
|
|
187
|
+
}
|
|
188
|
+
};
|
|
189
|
+
}
|
|
190
|
+
function walk(node, fileId, pyIds, symbols, edges, seen) {
|
|
191
|
+
for (const child of node.namedChildren) {
|
|
192
|
+
if (!child)
|
|
193
|
+
continue;
|
|
194
|
+
switch (child.type) {
|
|
195
|
+
case "import_statement":
|
|
196
|
+
for (const mod of importedModules(child)) {
|
|
197
|
+
const target = resolvePy(fileId, "", mod, pyIds);
|
|
198
|
+
if (target)
|
|
199
|
+
addEdge(edges, seen, fileId, target);
|
|
200
|
+
}
|
|
201
|
+
break;
|
|
202
|
+
case "import_from_statement": {
|
|
203
|
+
const { dots, mod } = fromModule(child);
|
|
204
|
+
const target = resolvePy(fileId, dots, mod, pyIds);
|
|
205
|
+
if (target)
|
|
206
|
+
addEdge(edges, seen, fileId, target);
|
|
207
|
+
break;
|
|
208
|
+
}
|
|
209
|
+
case "function_definition":
|
|
210
|
+
case "class_definition":
|
|
211
|
+
if (node.type === "module") {
|
|
212
|
+
const name = child.childForFieldName("name");
|
|
213
|
+
if (name) {
|
|
214
|
+
symbols.push({
|
|
215
|
+
name: name.text,
|
|
216
|
+
kind: child.type === "class_definition" ? "class" : "def"
|
|
217
|
+
});
|
|
218
|
+
}
|
|
219
|
+
}
|
|
220
|
+
break;
|
|
221
|
+
}
|
|
222
|
+
walk(child, fileId, pyIds, symbols, edges, seen);
|
|
223
|
+
}
|
|
224
|
+
}
|
|
225
|
+
function importedModules(node) {
|
|
226
|
+
const mods = [];
|
|
227
|
+
for (const c of node.namedChildren) {
|
|
228
|
+
if (!c)
|
|
229
|
+
continue;
|
|
230
|
+
if (c.type === "dotted_name")
|
|
231
|
+
mods.push(c.text);
|
|
232
|
+
else if (c.type === "aliased_import") {
|
|
233
|
+
const dn = c.namedChildren.find((x) => x?.type === "dotted_name");
|
|
234
|
+
if (dn)
|
|
235
|
+
mods.push(dn.text);
|
|
236
|
+
}
|
|
237
|
+
}
|
|
238
|
+
return mods;
|
|
239
|
+
}
|
|
240
|
+
function fromModule(node) {
|
|
241
|
+
for (const c of node.namedChildren) {
|
|
242
|
+
if (!c)
|
|
243
|
+
continue;
|
|
244
|
+
if (c.type === "dotted_name")
|
|
245
|
+
return { dots: "", mod: c.text };
|
|
246
|
+
if (c.type === "relative_import") {
|
|
247
|
+
const m = /^(\.+)(.*)$/.exec(c.text);
|
|
248
|
+
return { dots: m?.[1] ?? "", mod: m?.[2] ?? "" };
|
|
249
|
+
}
|
|
250
|
+
if (c.type === "import_list" || c.type === "wildcard_import")
|
|
251
|
+
break;
|
|
252
|
+
}
|
|
253
|
+
return { dots: "", mod: "" };
|
|
254
|
+
}
|
|
255
|
+
function addEdge(edges, seen, from, to) {
|
|
256
|
+
if (to === from || seen.has(to))
|
|
257
|
+
return;
|
|
258
|
+
seen.add(to);
|
|
259
|
+
edges.push({ from, to, kind: "import" });
|
|
260
|
+
}
|
|
261
|
+
function resolvePy(fromId, dots, mod, pyIds) {
|
|
262
|
+
let baseDir;
|
|
263
|
+
if (dots.length > 0) {
|
|
264
|
+
const parts = fromId.split("/");
|
|
265
|
+
parts.pop();
|
|
266
|
+
for (let i = 1;i < dots.length; i++)
|
|
267
|
+
parts.pop();
|
|
268
|
+
baseDir = parts;
|
|
269
|
+
} else {
|
|
270
|
+
baseDir = [];
|
|
271
|
+
}
|
|
272
|
+
const modParts = mod ? mod.split(".") : [];
|
|
273
|
+
const full = [...baseDir, ...modParts].filter(Boolean);
|
|
274
|
+
if (full.length === 0)
|
|
275
|
+
return null;
|
|
276
|
+
const stem = full.join("/");
|
|
277
|
+
for (const candidate of [`${stem}.py`, `${stem}/__init__.py`]) {
|
|
278
|
+
if (pyIds.has(candidate))
|
|
279
|
+
return candidate;
|
|
280
|
+
}
|
|
281
|
+
return null;
|
|
282
|
+
}
|
|
283
|
+
|
|
284
|
+
// packages/core/src/scan.ts
|
|
285
|
+
var TS_JS_EXT = new Set([".ts", ".tsx", ".js", ".jsx", ".mjs", ".cjs"]);
|
|
286
|
+
var SKIP_DIRS = new Set([
|
|
287
|
+
"node_modules",
|
|
288
|
+
".git",
|
|
289
|
+
"dist",
|
|
290
|
+
"build",
|
|
291
|
+
".next",
|
|
292
|
+
"coverage",
|
|
293
|
+
".codemap",
|
|
294
|
+
"__pycache__",
|
|
295
|
+
"venv",
|
|
296
|
+
".venv"
|
|
297
|
+
]);
|
|
298
|
+
async function scanRepo(rootInput, opts = {}) {
|
|
299
|
+
const root = path3.resolve(rootInput);
|
|
300
|
+
if (!fs2.existsSync(root) || !fs2.statSync(root).isDirectory()) {
|
|
301
|
+
throw new Error(`Not a directory: ${rootInput}`);
|
|
302
|
+
}
|
|
303
|
+
const tsFiles = [];
|
|
304
|
+
const pyFiles = [];
|
|
305
|
+
walk2(root, opts.outDirName, tsFiles, pyFiles);
|
|
306
|
+
const nodes = [];
|
|
307
|
+
const edges = [];
|
|
308
|
+
const calls = [];
|
|
309
|
+
if (tsFiles.length) {
|
|
310
|
+
const res = extractTsJs(root, tsFiles);
|
|
311
|
+
nodes.push(...res.nodes);
|
|
312
|
+
edges.push(...res.edges);
|
|
313
|
+
calls.push(...res.calls);
|
|
314
|
+
}
|
|
315
|
+
if (pyFiles.length) {
|
|
316
|
+
const pyIds = new Set(pyFiles.map((f) => toId(root, f)));
|
|
317
|
+
const python = await createPythonExtractor();
|
|
318
|
+
for (const f of pyFiles) {
|
|
319
|
+
const id = toId(root, f);
|
|
320
|
+
const content = fs2.readFileSync(assertInside(root, f), "utf8");
|
|
321
|
+
const { symbols, edges: e } = python.extract(id, content, pyIds);
|
|
322
|
+
nodes.push({ id, path: id, language: "python", symbols });
|
|
323
|
+
edges.push(...e);
|
|
324
|
+
}
|
|
325
|
+
}
|
|
326
|
+
nodes.sort((a, b) => a.id.localeCompare(b.id));
|
|
327
|
+
edges.sort((a, b) => a.from.localeCompare(b.from) || a.to.localeCompare(b.to));
|
|
328
|
+
calls.sort((a, b) => a.from.localeCompare(b.from) || a.fromSymbol.localeCompare(b.fromSymbol) || a.to.localeCompare(b.to) || a.toSymbol.localeCompare(b.toSymbol));
|
|
329
|
+
return { root, nodes, edges, calls };
|
|
330
|
+
}
|
|
331
|
+
function walk2(dir, outDirName, tsFiles, pyFiles) {
|
|
332
|
+
for (const entry of fs2.readdirSync(dir, { withFileTypes: true })) {
|
|
333
|
+
const full = path3.join(dir, entry.name);
|
|
334
|
+
if (entry.isDirectory()) {
|
|
335
|
+
if (entry.name.startsWith(".") || SKIP_DIRS.has(entry.name) || entry.name === outDirName) {
|
|
336
|
+
continue;
|
|
337
|
+
}
|
|
338
|
+
walk2(full, outDirName, tsFiles, pyFiles);
|
|
339
|
+
} else if (entry.isFile()) {
|
|
340
|
+
const ext = path3.extname(entry.name);
|
|
341
|
+
if (TS_JS_EXT.has(ext))
|
|
342
|
+
tsFiles.push(full);
|
|
343
|
+
else if (ext === ".py")
|
|
344
|
+
pyFiles.push(full);
|
|
345
|
+
}
|
|
346
|
+
}
|
|
347
|
+
}
|
|
348
|
+
// packages/core/src/vault/markdown.ts
|
|
349
|
+
import fs3 from "fs";
|
|
350
|
+
import path4 from "path";
|
|
351
|
+
function writeVault(graph, outDirInput) {
|
|
352
|
+
const outDir = assertSafeOutputDir(outDirInput);
|
|
353
|
+
fs3.mkdirSync(outDir, { recursive: true });
|
|
354
|
+
fs3.writeFileSync(path4.join(outDir, "graph.json"), JSON.stringify(graph, null, 2));
|
|
355
|
+
const deps = new Map;
|
|
356
|
+
const dependents = new Map;
|
|
357
|
+
for (const e of graph.edges) {
|
|
358
|
+
(deps.get(e.from) ?? deps.set(e.from, new Set).get(e.from)).add(e.to);
|
|
359
|
+
(dependents.get(e.to) ?? dependents.set(e.to, new Set).get(e.to)).add(e.from);
|
|
360
|
+
}
|
|
361
|
+
const callsOut = new Map;
|
|
362
|
+
const callsIn = new Map;
|
|
363
|
+
for (const c of graph.calls) {
|
|
364
|
+
(callsOut.get(c.from) ?? callsOut.set(c.from, []).get(c.from)).push(c);
|
|
365
|
+
(callsIn.get(c.to) ?? callsIn.set(c.to, []).get(c.to)).push(c);
|
|
366
|
+
}
|
|
367
|
+
let files = 0;
|
|
368
|
+
for (const node of graph.nodes) {
|
|
369
|
+
let notePath;
|
|
370
|
+
try {
|
|
371
|
+
notePath = assertInside(outDir, path4.join(outDir, `${node.id}.md`));
|
|
372
|
+
} catch {
|
|
373
|
+
continue;
|
|
374
|
+
}
|
|
375
|
+
fs3.mkdirSync(path4.dirname(notePath), { recursive: true });
|
|
376
|
+
fs3.writeFileSync(notePath, renderNote(node, [...deps.get(node.id) ?? []].sort(), [...dependents.get(node.id) ?? []].sort(), callsOut.get(node.id) ?? [], callsIn.get(node.id) ?? []));
|
|
377
|
+
files++;
|
|
378
|
+
}
|
|
379
|
+
return { files, outDir };
|
|
380
|
+
}
|
|
381
|
+
function list(ids) {
|
|
382
|
+
return ids.length ? ids.map((id) => `- [[${id}]]`).join(`
|
|
383
|
+
`) : "_none_";
|
|
384
|
+
}
|
|
385
|
+
function yamlList(items) {
|
|
386
|
+
return `[${items.map((s) => /[:#\[\]]/.test(s) ? JSON.stringify(s) : s).join(", ")}]`;
|
|
387
|
+
}
|
|
388
|
+
function callsOutList(calls) {
|
|
389
|
+
if (!calls.length)
|
|
390
|
+
return "_none_";
|
|
391
|
+
return calls.map((c) => `- \`${c.fromSymbol}\` \u2192 [[${c.to}]] \`${c.toSymbol}\``).join(`
|
|
392
|
+
`);
|
|
393
|
+
}
|
|
394
|
+
function callsInList(calls) {
|
|
395
|
+
if (!calls.length)
|
|
396
|
+
return "_none_";
|
|
397
|
+
return calls.map((c) => `- [[${c.from}]] \`${c.fromSymbol}\` \u2192 \`${c.toSymbol}\``).join(`
|
|
398
|
+
`);
|
|
399
|
+
}
|
|
400
|
+
function renderNote(node, deps, dependents, callsOut, callsIn) {
|
|
401
|
+
const symbols = node.symbols.map((s) => s.name);
|
|
402
|
+
const a = node.annotation;
|
|
403
|
+
const frontmatter = [
|
|
404
|
+
"---",
|
|
405
|
+
`path: ${node.path}`,
|
|
406
|
+
`language: ${node.language}`,
|
|
407
|
+
`symbols: ${yamlList(symbols)}`
|
|
408
|
+
];
|
|
409
|
+
if (a) {
|
|
410
|
+
frontmatter.push(`summary: ${JSON.stringify(a.summary)}`, `writes: ${yamlList(a.writes)}`, `reads: ${yamlList(a.reads)}`, `config: ${yamlList(a.config ?? [])}`, `auth: ${yamlList(a.auth)}`);
|
|
411
|
+
}
|
|
412
|
+
frontmatter.push("---", "");
|
|
413
|
+
const body = [`# ${node.path}`, ""];
|
|
414
|
+
if (a) {
|
|
415
|
+
body.push(a.summary, "");
|
|
416
|
+
if (a.flows.length) {
|
|
417
|
+
body.push("## Data flow", a.flows.map((f) => `- ${f}`).join(`
|
|
418
|
+
`), "");
|
|
419
|
+
}
|
|
420
|
+
}
|
|
421
|
+
body.push("## Dependencies", list(deps), "", "## Dependents", list(dependents), "");
|
|
422
|
+
if (callsOut.length || callsIn.length) {
|
|
423
|
+
body.push("", "## Calls", callsOutList(callsOut), "", "## Called by", callsInList(callsIn), "");
|
|
424
|
+
}
|
|
425
|
+
return [...frontmatter, ...body].join(`
|
|
426
|
+
`);
|
|
427
|
+
}
|
|
428
|
+
// packages/annotate/src/types.ts
|
|
429
|
+
import { z } from "zod";
|
|
430
|
+
var AnnotationSchema = z.object({
|
|
431
|
+
summary: z.string().catch(""),
|
|
432
|
+
writes: z.array(z.string()).catch([]),
|
|
433
|
+
reads: z.array(z.string()).catch([]),
|
|
434
|
+
config: z.array(z.string()).catch([]),
|
|
435
|
+
auth: z.array(z.string()).catch([]),
|
|
436
|
+
flows: z.array(z.string()).catch([])
|
|
437
|
+
});
|
|
438
|
+
|
|
439
|
+
// packages/annotate/src/prompt.ts
|
|
440
|
+
var MAX_SOURCE_CHARS = 12000;
|
|
441
|
+
var SYSTEM_PROMPT = "Given one source file, describe its role and the data flows it participates in. Be concrete and terse. " + "Never invent dependencies. Respond ONLY with a single JSON object. No prose, no code fences.";
|
|
442
|
+
function buildUserPrompt(input) {
|
|
443
|
+
const source = input.source.length > MAX_SOURCE_CHARS ? `${input.source.slice(0, MAX_SOURCE_CHARS)}
|
|
444
|
+
\u2026(truncated)` : input.source;
|
|
445
|
+
return [
|
|
446
|
+
`File: ${input.id} (${input.language})`,
|
|
447
|
+
input.dependencies.length ? `Imports: ${input.dependencies.join(", ")}` : "",
|
|
448
|
+
"",
|
|
449
|
+
"Source:",
|
|
450
|
+
"```",
|
|
451
|
+
source,
|
|
452
|
+
"```",
|
|
453
|
+
"",
|
|
454
|
+
"Return JSON with exactly these keys:",
|
|
455
|
+
'{"summary": string, "writes": string[], "reads": string[], "config": string[], "auth": string[], "flows": string[]}',
|
|
456
|
+
"- summary: 1-2 sentences on what this file does.",
|
|
457
|
+
"- writes: data stores this code WRITES to \u2014 DB tables/collections, caches, queues, topics.",
|
|
458
|
+
" Bare names only (e.g. users, sessions). EXCLUDE env vars, file/module paths, and connection handles.",
|
|
459
|
+
"- reads: data stores this code READS from \u2014 same rules as writes.",
|
|
460
|
+
"- config: environment variables / config / secrets this code reads (e.g. DATABASE_URL, PORT).",
|
|
461
|
+
"- auth: authentication/authorization gates enforced or required here.",
|
|
462
|
+
"- flows: short phrases describing how data moves through this file.",
|
|
463
|
+
"Use [] for any category that does not apply."
|
|
464
|
+
].filter(Boolean).join(`
|
|
465
|
+
`);
|
|
466
|
+
}
|
|
467
|
+
|
|
468
|
+
// packages/annotate/src/providers/openai-compat.ts
|
|
469
|
+
class OpenAICompatAnnotator {
|
|
470
|
+
cfg;
|
|
471
|
+
model;
|
|
472
|
+
constructor(cfg) {
|
|
473
|
+
this.cfg = cfg;
|
|
474
|
+
this.model = cfg.model;
|
|
475
|
+
}
|
|
476
|
+
async annotate(input) {
|
|
477
|
+
const headers = {
|
|
478
|
+
"content-type": "application/json",
|
|
479
|
+
...this.cfg.headers
|
|
480
|
+
};
|
|
481
|
+
if (this.cfg.apiKey)
|
|
482
|
+
headers.authorization = `Bearer ${this.cfg.apiKey}`;
|
|
483
|
+
const body = {
|
|
484
|
+
model: this.cfg.model,
|
|
485
|
+
temperature: 0,
|
|
486
|
+
messages: [
|
|
487
|
+
{ role: "system", content: SYSTEM_PROMPT },
|
|
488
|
+
{ role: "user", content: buildUserPrompt(input) }
|
|
489
|
+
],
|
|
490
|
+
...this.cfg.jsonMode === false ? {} : { response_format: { type: "json_object" } }
|
|
491
|
+
};
|
|
492
|
+
const url = `${this.cfg.baseUrl.replace(/\/+$/, "")}/chat/completions`;
|
|
493
|
+
const res = await fetch(url, { method: "POST", headers, body: JSON.stringify(body) });
|
|
494
|
+
if (!res.ok) {
|
|
495
|
+
const detail = await res.text().catch(() => "");
|
|
496
|
+
const err = new Error(`LLM request failed (${res.status}): ${detail.slice(0, 300)}`);
|
|
497
|
+
err.status = res.status;
|
|
498
|
+
throw err;
|
|
499
|
+
}
|
|
500
|
+
const data = await res.json();
|
|
501
|
+
return parseAnnotation(data.choices?.[0]?.message?.content ?? "");
|
|
502
|
+
}
|
|
503
|
+
}
|
|
504
|
+
function parseAnnotation(content) {
|
|
505
|
+
return AnnotationSchema.parse(extractJson(content));
|
|
506
|
+
}
|
|
507
|
+
function extractJson(content) {
|
|
508
|
+
const trimmed = content.trim();
|
|
509
|
+
try {
|
|
510
|
+
return JSON.parse(trimmed);
|
|
511
|
+
} catch {
|
|
512
|
+
const match = /\{[\s\S]*\}/.exec(trimmed);
|
|
513
|
+
if (match)
|
|
514
|
+
return JSON.parse(match[0]);
|
|
515
|
+
throw new Error("Model did not return valid JSON");
|
|
516
|
+
}
|
|
517
|
+
}
|
|
518
|
+
|
|
519
|
+
// packages/annotate/src/config.ts
|
|
520
|
+
var DEFAULT_MODELS = {
|
|
521
|
+
mistral: "devstral-small-2512",
|
|
522
|
+
ollama: "qwen2.5-coder:7b",
|
|
523
|
+
openai: "gpt-4o-mini"
|
|
524
|
+
};
|
|
525
|
+
function createAnnotator(opts = {}) {
|
|
526
|
+
const provider = opts.provider ?? process.env.CODEMAP_PROVIDER ?? "ollama";
|
|
527
|
+
const model = opts.model ?? process.env.CODEMAP_MODEL ?? DEFAULT_MODELS[provider];
|
|
528
|
+
const baseUrl = opts.baseUrl ?? process.env.CODEMAP_BASE_URL;
|
|
529
|
+
const apiKey = opts.apiKey ?? process.env.CODEMAP_API_KEY ?? envKey(provider);
|
|
530
|
+
switch (provider) {
|
|
531
|
+
case "mistral":
|
|
532
|
+
requireKey(apiKey, "mistral", "MISTRAL_API_KEY");
|
|
533
|
+
return new OpenAICompatAnnotator({
|
|
534
|
+
baseUrl: baseUrl ?? "https://api.mistral.ai/v1",
|
|
535
|
+
model,
|
|
536
|
+
apiKey
|
|
537
|
+
});
|
|
538
|
+
case "openai":
|
|
539
|
+
requireKey(apiKey, "openai", "OPENAI_API_KEY");
|
|
540
|
+
return new OpenAICompatAnnotator({
|
|
541
|
+
baseUrl: baseUrl ?? "https://api.openai.com/v1",
|
|
542
|
+
model,
|
|
543
|
+
apiKey
|
|
544
|
+
});
|
|
545
|
+
case "ollama":
|
|
546
|
+
return new OpenAICompatAnnotator({
|
|
547
|
+
baseUrl: baseUrl ?? "http://localhost:11434/v1",
|
|
548
|
+
model
|
|
549
|
+
});
|
|
550
|
+
default:
|
|
551
|
+
throw new Error(`Unknown provider: ${String(provider)}`);
|
|
552
|
+
}
|
|
553
|
+
}
|
|
554
|
+
function envKey(provider) {
|
|
555
|
+
if (provider === "mistral")
|
|
556
|
+
return process.env.MISTRAL_API_KEY;
|
|
557
|
+
if (provider === "openai")
|
|
558
|
+
return process.env.OPENAI_API_KEY;
|
|
559
|
+
return;
|
|
560
|
+
}
|
|
561
|
+
function requireKey(key, provider, envName) {
|
|
562
|
+
if (!key) {
|
|
563
|
+
throw new Error(`Provider "${provider}" needs an API key. Set CODEMAP_API_KEY or ${envName}.`);
|
|
564
|
+
}
|
|
565
|
+
}
|
|
566
|
+
// packages/annotate/src/annotate.ts
|
|
567
|
+
import fs5 from "fs";
|
|
568
|
+
import path6 from "path";
|
|
569
|
+
|
|
570
|
+
// packages/annotate/src/cache.ts
|
|
571
|
+
import { createHash } from "crypto";
|
|
572
|
+
import fs4 from "fs";
|
|
573
|
+
import path5 from "path";
|
|
574
|
+
var ANNOTATION_SCHEMA_VERSION = "2";
|
|
575
|
+
|
|
576
|
+
class AnnotationCache {
|
|
577
|
+
file;
|
|
578
|
+
data = {};
|
|
579
|
+
constructor(cacheDir) {
|
|
580
|
+
this.file = path5.join(cacheDir, "annotations.json");
|
|
581
|
+
try {
|
|
582
|
+
this.data = JSON.parse(fs4.readFileSync(this.file, "utf8"));
|
|
583
|
+
} catch {
|
|
584
|
+
this.data = {};
|
|
585
|
+
}
|
|
586
|
+
}
|
|
587
|
+
static key(model, source) {
|
|
588
|
+
return createHash("sha256").update(ANNOTATION_SCHEMA_VERSION).update("\x00").update(model).update("\x00").update(source).digest("hex");
|
|
589
|
+
}
|
|
590
|
+
get(key) {
|
|
591
|
+
return this.data[key];
|
|
592
|
+
}
|
|
593
|
+
set(key, annotation) {
|
|
594
|
+
this.data[key] = annotation;
|
|
595
|
+
}
|
|
596
|
+
save() {
|
|
597
|
+
fs4.mkdirSync(path5.dirname(this.file), { recursive: true });
|
|
598
|
+
fs4.writeFileSync(this.file, JSON.stringify(this.data, null, 2));
|
|
599
|
+
}
|
|
600
|
+
}
|
|
601
|
+
|
|
602
|
+
// packages/annotate/src/annotate.ts
|
|
603
|
+
async function annotateGraph(outDir, annotator, opts = {}) {
|
|
604
|
+
const graphPath = path6.join(outDir, "graph.json");
|
|
605
|
+
const graph = JSON.parse(fs5.readFileSync(graphPath, "utf8"));
|
|
606
|
+
const deps = new Map;
|
|
607
|
+
const dependents = new Map;
|
|
608
|
+
for (const e of graph.edges) {
|
|
609
|
+
(deps.get(e.from) ?? deps.set(e.from, []).get(e.from)).push(e.to);
|
|
610
|
+
(dependents.get(e.to) ?? dependents.set(e.to, []).get(e.to)).push(e.from);
|
|
611
|
+
}
|
|
612
|
+
const cache = new AnnotationCache(path6.join(outDir, ".cache"));
|
|
613
|
+
const result = {
|
|
614
|
+
annotated: 0,
|
|
615
|
+
cached: 0,
|
|
616
|
+
failed: 0,
|
|
617
|
+
total: graph.nodes.length
|
|
618
|
+
};
|
|
619
|
+
const queue = [...graph.nodes];
|
|
620
|
+
let done = 0;
|
|
621
|
+
const workers = Math.max(1, opts.concurrency ?? 4);
|
|
622
|
+
async function worker() {
|
|
623
|
+
for (;; ) {
|
|
624
|
+
const node = queue.shift();
|
|
625
|
+
if (!node)
|
|
626
|
+
return;
|
|
627
|
+
try {
|
|
628
|
+
const abs = assertInside(graph.root, path6.join(graph.root, node.id));
|
|
629
|
+
const source = fs5.readFileSync(abs, "utf8");
|
|
630
|
+
const key = AnnotationCache.key(annotator.model, source);
|
|
631
|
+
const hit = cache.get(key);
|
|
632
|
+
if (hit) {
|
|
633
|
+
node.annotation = hit;
|
|
634
|
+
result.cached++;
|
|
635
|
+
} else {
|
|
636
|
+
const annotation = await withRetry(() => annotator.annotate({
|
|
637
|
+
id: node.id,
|
|
638
|
+
language: node.language,
|
|
639
|
+
source,
|
|
640
|
+
dependencies: deps.get(node.id) ?? [],
|
|
641
|
+
dependents: dependents.get(node.id) ?? []
|
|
642
|
+
}));
|
|
643
|
+
node.annotation = annotation;
|
|
644
|
+
cache.set(key, annotation);
|
|
645
|
+
result.annotated++;
|
|
646
|
+
}
|
|
647
|
+
} catch {
|
|
648
|
+
result.failed++;
|
|
649
|
+
} finally {
|
|
650
|
+
done++;
|
|
651
|
+
opts.onProgress?.(done, result.total, node.id);
|
|
652
|
+
}
|
|
653
|
+
}
|
|
654
|
+
}
|
|
655
|
+
await Promise.all(Array.from({ length: workers }, () => worker()));
|
|
656
|
+
cache.save();
|
|
657
|
+
fs5.writeFileSync(graphPath, JSON.stringify(graph, null, 2));
|
|
658
|
+
writeVault(graph, outDir);
|
|
659
|
+
return result;
|
|
660
|
+
}
|
|
661
|
+
async function withRetry(fn, attempts = 3) {
|
|
662
|
+
let lastErr;
|
|
663
|
+
for (let i = 0;i < attempts; i++) {
|
|
664
|
+
try {
|
|
665
|
+
return await fn();
|
|
666
|
+
} catch (err) {
|
|
667
|
+
lastErr = err;
|
|
668
|
+
if (i === attempts - 1 || !isRetryable(err))
|
|
669
|
+
throw err;
|
|
670
|
+
const delay = 250 * 2 ** i * (0.5 + Math.random());
|
|
671
|
+
await new Promise((r) => setTimeout(r, delay));
|
|
672
|
+
}
|
|
673
|
+
}
|
|
674
|
+
throw lastErr;
|
|
675
|
+
}
|
|
676
|
+
function isRetryable(err) {
|
|
677
|
+
const status = err?.status;
|
|
678
|
+
if (status === undefined)
|
|
679
|
+
return true;
|
|
680
|
+
return status === 429 || status >= 500;
|
|
681
|
+
}
|
|
682
|
+
// packages/cli/src/serve.ts
|
|
683
|
+
import fs6 from "fs";
|
|
684
|
+
import os from "os";
|
|
685
|
+
import path7 from "path";
|
|
686
|
+
function expandHome(p) {
|
|
687
|
+
if (p === "~")
|
|
688
|
+
return os.homedir();
|
|
689
|
+
if (p.startsWith("~/") || p.startsWith("~\\"))
|
|
690
|
+
return path7.join(os.homedir(), p.slice(2));
|
|
691
|
+
return p;
|
|
692
|
+
}
|
|
693
|
+
function guardLocal(req, port) {
|
|
694
|
+
const allowedHosts = new Set([
|
|
695
|
+
`localhost:${port}`,
|
|
696
|
+
`127.0.0.1:${port}`,
|
|
697
|
+
`[::1]:${port}`
|
|
698
|
+
]);
|
|
699
|
+
const host = req.headers.get("host");
|
|
700
|
+
if (!host || !allowedHosts.has(host)) {
|
|
701
|
+
return new Response("Forbidden: unexpected Host header.", { status: 403 });
|
|
702
|
+
}
|
|
703
|
+
const origin = req.headers.get("origin");
|
|
704
|
+
if (origin) {
|
|
705
|
+
let originHost;
|
|
706
|
+
try {
|
|
707
|
+
originHost = new URL(origin).host;
|
|
708
|
+
} catch {
|
|
709
|
+
return new Response("Forbidden: malformed Origin.", { status: 403 });
|
|
710
|
+
}
|
|
711
|
+
if (!allowedHosts.has(originHost)) {
|
|
712
|
+
return new Response("Forbidden: cross-origin request blocked.", { status: 403 });
|
|
713
|
+
}
|
|
714
|
+
}
|
|
715
|
+
return null;
|
|
716
|
+
}
|
|
717
|
+
function createMutex() {
|
|
718
|
+
let tail = Promise.resolve();
|
|
719
|
+
return (fn) => {
|
|
720
|
+
const run = tail.then(fn, fn);
|
|
721
|
+
tail = run.then(() => {
|
|
722
|
+
return;
|
|
723
|
+
}, () => {
|
|
724
|
+
return;
|
|
725
|
+
});
|
|
726
|
+
return run;
|
|
727
|
+
};
|
|
728
|
+
}
|
|
729
|
+
async function serve(vaultInput, opts = {}) {
|
|
730
|
+
const webDir = opts.webDir ? path7.resolve(opts.webDir) : resolveWebDir();
|
|
731
|
+
await ensureWebBuilt(webDir);
|
|
732
|
+
let currentVault = null;
|
|
733
|
+
if (vaultInput) {
|
|
734
|
+
const v = path7.resolve(vaultInput);
|
|
735
|
+
if (fs6.existsSync(path7.join(v, "graph.json")))
|
|
736
|
+
currentVault = v;
|
|
737
|
+
else
|
|
738
|
+
console.warn(`No graph.json in ${v} yet \u2014 scan a directory from the UI.`);
|
|
739
|
+
}
|
|
740
|
+
const port = opts.port ?? 4321;
|
|
741
|
+
const withVaultLock = createMutex();
|
|
742
|
+
Bun.serve({
|
|
743
|
+
port,
|
|
744
|
+
hostname: "127.0.0.1",
|
|
745
|
+
idleTimeout: 0,
|
|
746
|
+
async fetch(req) {
|
|
747
|
+
const blocked = guardLocal(req, port);
|
|
748
|
+
if (blocked)
|
|
749
|
+
return blocked;
|
|
750
|
+
const { pathname } = new URL(req.url);
|
|
751
|
+
if (pathname === "/api/health") {
|
|
752
|
+
return Response.json({ ok: true, vault: currentVault });
|
|
753
|
+
}
|
|
754
|
+
if (pathname === "/graph.json") {
|
|
755
|
+
if (!currentVault)
|
|
756
|
+
return new Response("No vault scanned yet.", { status: 404 });
|
|
757
|
+
return new Response(Bun.file(path7.join(currentVault, "graph.json")), {
|
|
758
|
+
headers: { "content-type": "application/json", "cache-control": "no-store" }
|
|
759
|
+
});
|
|
760
|
+
}
|
|
761
|
+
if (pathname === "/api/ls") {
|
|
762
|
+
return handleLs(new URL(req.url).searchParams.get("path"));
|
|
763
|
+
}
|
|
764
|
+
if (pathname === "/api/scan" && req.method === "POST") {
|
|
765
|
+
return withVaultLock(async () => {
|
|
766
|
+
const { response, vault } = await handleScan(req);
|
|
767
|
+
if (vault)
|
|
768
|
+
currentVault = vault;
|
|
769
|
+
return response;
|
|
770
|
+
});
|
|
771
|
+
}
|
|
772
|
+
if (pathname === "/api/annotate" && req.method === "POST") {
|
|
773
|
+
return handleAnnotate(req, currentVault, withVaultLock);
|
|
774
|
+
}
|
|
775
|
+
return serveStatic(webDir, pathname);
|
|
776
|
+
}
|
|
777
|
+
});
|
|
778
|
+
console.log(`code-mapper UI \u2192 http://localhost:${port}`);
|
|
779
|
+
if (currentVault)
|
|
780
|
+
console.log(` vault: ${currentVault}`);
|
|
781
|
+
else
|
|
782
|
+
console.log(" scan a directory from the browser to begin");
|
|
783
|
+
}
|
|
784
|
+
function handleLs(query) {
|
|
785
|
+
const target = query?.trim() ? expandHome(query.trim()) : process.cwd();
|
|
786
|
+
const dir = path7.resolve(target);
|
|
787
|
+
let stat;
|
|
788
|
+
try {
|
|
789
|
+
stat = fs6.statSync(dir);
|
|
790
|
+
} catch {
|
|
791
|
+
return Response.json({ error: `Cannot access: ${dir}` }, { status: 400 });
|
|
792
|
+
}
|
|
793
|
+
if (!stat.isDirectory()) {
|
|
794
|
+
return Response.json({ error: `Not a directory: ${dir}` }, { status: 400 });
|
|
795
|
+
}
|
|
796
|
+
let entries = [];
|
|
797
|
+
try {
|
|
798
|
+
entries = fs6.readdirSync(dir, { withFileTypes: true }).filter((e) => {
|
|
799
|
+
try {
|
|
800
|
+
if (e.isDirectory())
|
|
801
|
+
return true;
|
|
802
|
+
return e.isSymbolicLink() && fs6.statSync(path7.join(dir, e.name)).isDirectory();
|
|
803
|
+
} catch {
|
|
804
|
+
return false;
|
|
805
|
+
}
|
|
806
|
+
}).map((e) => ({ name: e.name, path: path7.join(dir, e.name) })).sort((a, b) => a.name.localeCompare(b.name));
|
|
807
|
+
} catch (err) {
|
|
808
|
+
return Response.json({ error: err.message }, { status: 400 });
|
|
809
|
+
}
|
|
810
|
+
const parent = path7.dirname(dir);
|
|
811
|
+
return Response.json({ path: dir, parent: parent === dir ? null : parent, entries });
|
|
812
|
+
}
|
|
813
|
+
async function handleScan(req) {
|
|
814
|
+
let body;
|
|
815
|
+
try {
|
|
816
|
+
body = await req.json();
|
|
817
|
+
} catch {
|
|
818
|
+
return { response: Response.json({ error: "Invalid JSON body." }, { status: 400 }) };
|
|
819
|
+
}
|
|
820
|
+
const target = body.path?.trim();
|
|
821
|
+
if (!target) {
|
|
822
|
+
return { response: Response.json({ error: "A directory path is required." }, { status: 400 }) };
|
|
823
|
+
}
|
|
824
|
+
const root = path7.resolve(expandHome(target));
|
|
825
|
+
if (!fs6.existsSync(root)) {
|
|
826
|
+
return { response: Response.json({ error: `Path not found: ${root}` }, { status: 400 }) };
|
|
827
|
+
}
|
|
828
|
+
if (!fs6.statSync(root).isDirectory()) {
|
|
829
|
+
return { response: Response.json({ error: `Not a directory: ${root}` }, { status: 400 }) };
|
|
830
|
+
}
|
|
831
|
+
const out = body.out?.trim() ? path7.resolve(body.out) : path7.join(root, ".codemap");
|
|
832
|
+
try {
|
|
833
|
+
const graph = await scanRepo(root, { outDirName: path7.basename(out) });
|
|
834
|
+
const { files } = writeVault(graph, out);
|
|
835
|
+
return {
|
|
836
|
+
response: Response.json({
|
|
837
|
+
files,
|
|
838
|
+
nodes: graph.nodes.length,
|
|
839
|
+
edges: graph.edges.length,
|
|
840
|
+
calls: graph.calls.length,
|
|
841
|
+
vault: out
|
|
842
|
+
}),
|
|
843
|
+
vault: out
|
|
844
|
+
};
|
|
845
|
+
} catch (err) {
|
|
846
|
+
return { response: Response.json({ error: err.message }, { status: 500 }) };
|
|
847
|
+
}
|
|
848
|
+
}
|
|
849
|
+
async function handleAnnotate(req, currentVault, lock) {
|
|
850
|
+
if (!currentVault) {
|
|
851
|
+
return Response.json({ error: "Scan a directory first." }, { status: 400 });
|
|
852
|
+
}
|
|
853
|
+
let body;
|
|
854
|
+
try {
|
|
855
|
+
body = await req.json();
|
|
856
|
+
} catch {
|
|
857
|
+
return Response.json({ error: "Invalid JSON body." }, { status: 400 });
|
|
858
|
+
}
|
|
859
|
+
const model = body.model?.trim();
|
|
860
|
+
if (!model) {
|
|
861
|
+
return Response.json({ error: "A model name is required." }, { status: 400 });
|
|
862
|
+
}
|
|
863
|
+
const baseUrl = body.baseUrl?.trim() || "http://localhost:11434/v1";
|
|
864
|
+
const apiKey = body.apiKey?.trim() || undefined;
|
|
865
|
+
const isLocal = /\/\/(localhost|127\.0\.0\.1)(:|\/|$)/.test(baseUrl);
|
|
866
|
+
const concurrency = isLocal ? 1 : 4;
|
|
867
|
+
const stream = new ReadableStream({
|
|
868
|
+
async start(controller) {
|
|
869
|
+
const enc = new TextEncoder;
|
|
870
|
+
const send = (o) => controller.enqueue(enc.encode(`${JSON.stringify(o)}
|
|
871
|
+
`));
|
|
872
|
+
try {
|
|
873
|
+
await lock(async () => {
|
|
874
|
+
const annotator = new OpenAICompatAnnotator({ baseUrl, model, apiKey });
|
|
875
|
+
const result = await annotateGraph(currentVault, annotator, {
|
|
876
|
+
concurrency,
|
|
877
|
+
onProgress: (done, total, file) => send({ type: "progress", done, total, file })
|
|
878
|
+
});
|
|
879
|
+
const message = result.total > 0 && result.failed === result.total ? isLocal ? `Every file failed \u2014 is a server running at ${baseUrl}? (e.g. \`ollama serve\` + \`ollama pull ${model}\`)` : `Every file failed \u2014 check the base URL, API key, and model (${model}).` : `Annotated ${result.annotated}, cached ${result.cached}, failed ${result.failed} (${model}).`;
|
|
880
|
+
send({ type: "done", ...result, model, message });
|
|
881
|
+
});
|
|
882
|
+
} catch (err) {
|
|
883
|
+
send({ type: "error", error: err.message });
|
|
884
|
+
} finally {
|
|
885
|
+
controller.close();
|
|
886
|
+
}
|
|
887
|
+
}
|
|
888
|
+
});
|
|
889
|
+
return new Response(stream, {
|
|
890
|
+
headers: { "content-type": "application/x-ndjson", "cache-control": "no-store" }
|
|
891
|
+
});
|
|
892
|
+
}
|
|
893
|
+
function serveStatic(webDir, pathname) {
|
|
894
|
+
const rel = pathname === "/" ? "index.html" : pathname.replace(/^\/+/, "");
|
|
895
|
+
const resolved = path7.resolve(webDir, rel);
|
|
896
|
+
if (resolved !== webDir && !resolved.startsWith(webDir + path7.sep)) {
|
|
897
|
+
return new Response("Forbidden", { status: 403 });
|
|
898
|
+
}
|
|
899
|
+
if (fs6.existsSync(resolved) && fs6.statSync(resolved).isFile()) {
|
|
900
|
+
return new Response(Bun.file(resolved));
|
|
901
|
+
}
|
|
902
|
+
return new Response(Bun.file(path7.join(webDir, "index.html")), {
|
|
903
|
+
headers: { "content-type": "text/html" }
|
|
904
|
+
});
|
|
905
|
+
}
|
|
906
|
+
function resolveWebDir() {
|
|
907
|
+
const candidates = [
|
|
908
|
+
path7.resolve(import.meta.dir, "../../web/dist"),
|
|
909
|
+
path7.resolve(import.meta.dir, "../web")
|
|
910
|
+
];
|
|
911
|
+
for (const c of candidates) {
|
|
912
|
+
if (fs6.existsSync(path7.join(c, "index.html")))
|
|
913
|
+
return c;
|
|
914
|
+
}
|
|
915
|
+
return candidates[0];
|
|
916
|
+
}
|
|
917
|
+
async function ensureWebBuilt(webDir) {
|
|
918
|
+
if (fs6.existsSync(path7.join(webDir, "index.html")))
|
|
919
|
+
return;
|
|
920
|
+
const webPkg = path7.resolve(webDir, "..");
|
|
921
|
+
if (!fs6.existsSync(path7.join(webPkg, "vite.config.ts"))) {
|
|
922
|
+
throw new Error(`Web UI assets missing at ${webDir}. Try reinstalling code-mapper.`);
|
|
923
|
+
}
|
|
924
|
+
console.log("Building web UI (first run)\u2026");
|
|
925
|
+
const proc = Bun.spawn(["bunx", "vite", "build", webPkg], { stdout: "inherit", stderr: "inherit" });
|
|
926
|
+
const code = await proc.exited;
|
|
927
|
+
if (code !== 0 || !fs6.existsSync(path7.join(webDir, "index.html"))) {
|
|
928
|
+
throw new Error(`Failed to build the web UI at ${webPkg}.`);
|
|
929
|
+
}
|
|
930
|
+
}
|
|
931
|
+
|
|
932
|
+
// packages/cli/src/index.ts
|
|
933
|
+
var program = new Command;
|
|
934
|
+
program.name("codemap").description("Map your codebase's dependencies and data flows into an Obsidian-linkable vault").version("0.1.0");
|
|
935
|
+
program.command("scan").description("Scan a directory and generate the dependency graph + Markdown vault").argument("<dir>", "target directory to scan").option("-o, --out <dir>", "output vault directory", ".codemap").action(async (dir, options) => {
|
|
936
|
+
const graph = await scanRepo(dir, { outDirName: path8.basename(options.out) });
|
|
937
|
+
const { files, outDir } = writeVault(graph, options.out);
|
|
938
|
+
console.log(`Scanned ${graph.nodes.length} files, ${graph.edges.length} import edges.`);
|
|
939
|
+
console.log(`Wrote ${files} notes + graph.json -> ${outDir}`);
|
|
940
|
+
});
|
|
941
|
+
program.command("annotate").description("Add the LLM semantic layer (summary, writes/reads/auth/flows) to an existing vault").option("-o, --out <dir>", "vault directory", ".codemap").option("-p, --provider <name>", "ollama | mistral | openai").option("-m, --model <id>", "model id override").option("--base-url <url>", "custom OpenAI-compatible endpoint").action(async (options) => {
|
|
942
|
+
const annotator = createAnnotator({
|
|
943
|
+
provider: options.provider,
|
|
944
|
+
model: options.model,
|
|
945
|
+
baseUrl: options.baseUrl
|
|
946
|
+
});
|
|
947
|
+
console.log(`Annotating vault "${options.out}" with model "${annotator.model}"\u2026`);
|
|
948
|
+
const res = await annotateGraph(options.out, annotator, {
|
|
949
|
+
onProgress: (done, total, id) => {
|
|
950
|
+
const label = id.length > 44 ? `\u2026${id.slice(-43)}` : id.padEnd(44);
|
|
951
|
+
process.stdout.write(`\r [${done}/${total}] ${label}`);
|
|
952
|
+
}
|
|
953
|
+
});
|
|
954
|
+
process.stdout.write(`
|
|
955
|
+
`);
|
|
956
|
+
console.log(`Done: ${res.annotated} annotated, ${res.cached} cached, ${res.failed} failed (of ${res.total}).`);
|
|
957
|
+
});
|
|
958
|
+
program.command("serve").description("Launch the web UI \u2014 scan, annotate, and explore from the browser").argument("[vault]", "existing vault to open (optional; you can scan from the UI)").option("--port <n>", "port to listen on", "4321").option("--web <dir>", "path to the built web UI (defaults to packages/web/dist)").action(async (vault, options) => {
|
|
959
|
+
await serve(vault, { port: Number(options.port), webDir: options.web });
|
|
960
|
+
});
|
|
961
|
+
program.parseAsync();
|