@optave/codegraph 3.0.3 → 3.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.
@@ -0,0 +1,369 @@
1
+ /**
2
+ * Sequence diagram generation – Mermaid sequenceDiagram from call graph edges.
3
+ *
4
+ * Participants are files (not individual functions). Calls within the same file
5
+ * become self-messages. This keeps diagrams readable and matches typical
6
+ * sequence-diagram conventions.
7
+ */
8
+
9
+ import { openReadonlyOrFail } from './db.js';
10
+ import { paginateResult, printNdjson } from './paginate.js';
11
+ import { findMatchingNodes, isTestFile, kindIcon } from './queries.js';
12
+ import { FRAMEWORK_ENTRY_PREFIXES } from './structure.js';
13
+
14
+ // ─── Alias generation ────────────────────────────────────────────────
15
+
16
+ /**
17
+ * Build short participant aliases from file paths with collision handling.
18
+ * e.g. "src/builder.js" → "builder", but if two files share basename,
19
+ * progressively add parent dirs: "src/builder" vs "lib/builder".
20
+ */
21
+ function buildAliases(files) {
22
+ const aliases = new Map();
23
+ const basenames = new Map();
24
+
25
+ // Group by basename
26
+ for (const file of files) {
27
+ const base = file
28
+ .split('/')
29
+ .pop()
30
+ .replace(/\.[^.]+$/, '');
31
+ if (!basenames.has(base)) basenames.set(base, []);
32
+ basenames.get(base).push(file);
33
+ }
34
+
35
+ for (const [base, paths] of basenames) {
36
+ if (paths.length === 1) {
37
+ aliases.set(paths[0], base);
38
+ } else {
39
+ // Collision — progressively add parent dirs until aliases are unique
40
+ for (let depth = 2; depth <= 10; depth++) {
41
+ const trial = new Map();
42
+ let allUnique = true;
43
+ const seen = new Set();
44
+
45
+ for (const p of paths) {
46
+ const parts = p.replace(/\.[^.]+$/, '').split('/');
47
+ const alias = parts
48
+ .slice(-depth)
49
+ .join('_')
50
+ .replace(/[^a-zA-Z0-9_-]/g, '_');
51
+ trial.set(p, alias);
52
+ if (seen.has(alias)) allUnique = false;
53
+ seen.add(alias);
54
+ }
55
+
56
+ if (allUnique || depth === 10) {
57
+ for (const [p, alias] of trial) {
58
+ aliases.set(p, alias);
59
+ }
60
+ break;
61
+ }
62
+ }
63
+ }
64
+ }
65
+
66
+ return aliases;
67
+ }
68
+
69
+ // ─── Core data function ──────────────────────────────────────────────
70
+
71
+ /**
72
+ * Build sequence diagram data by BFS-forward from an entry point.
73
+ *
74
+ * @param {string} name - Symbol name to trace from
75
+ * @param {string} [dbPath]
76
+ * @param {object} [opts]
77
+ * @param {number} [opts.depth=10]
78
+ * @param {boolean} [opts.noTests]
79
+ * @param {string} [opts.file]
80
+ * @param {string} [opts.kind]
81
+ * @param {boolean} [opts.dataflow]
82
+ * @param {number} [opts.limit]
83
+ * @param {number} [opts.offset]
84
+ * @returns {{ entry, participants, messages, depth, totalMessages, truncated }}
85
+ */
86
+ export function sequenceData(name, dbPath, opts = {}) {
87
+ const db = openReadonlyOrFail(dbPath);
88
+ const maxDepth = opts.depth || 10;
89
+ const noTests = opts.noTests || false;
90
+ const withDataflow = opts.dataflow || false;
91
+
92
+ // Phase 1: Direct LIKE match
93
+ let matchNode = findMatchingNodes(db, name, opts)[0] ?? null;
94
+
95
+ // Phase 2: Prefix-stripped matching
96
+ if (!matchNode) {
97
+ for (const prefix of FRAMEWORK_ENTRY_PREFIXES) {
98
+ matchNode = findMatchingNodes(db, `${prefix}${name}`, opts)[0] ?? null;
99
+ if (matchNode) break;
100
+ }
101
+ }
102
+
103
+ if (!matchNode) {
104
+ db.close();
105
+ return {
106
+ entry: null,
107
+ participants: [],
108
+ messages: [],
109
+ depth: maxDepth,
110
+ totalMessages: 0,
111
+ truncated: false,
112
+ };
113
+ }
114
+
115
+ const entry = {
116
+ name: matchNode.name,
117
+ file: matchNode.file,
118
+ kind: matchNode.kind,
119
+ line: matchNode.line,
120
+ };
121
+
122
+ // BFS forward — track edges, not just nodes
123
+ const visited = new Set([matchNode.id]);
124
+ let frontier = [matchNode.id];
125
+ const messages = [];
126
+ const fileSet = new Set([matchNode.file]);
127
+ const idToNode = new Map();
128
+ idToNode.set(matchNode.id, matchNode);
129
+ let truncated = false;
130
+
131
+ const getCallees = db.prepare(
132
+ `SELECT DISTINCT n.id, n.name, n.kind, n.file, n.line
133
+ FROM edges e JOIN nodes n ON e.target_id = n.id
134
+ WHERE e.source_id = ? AND e.kind = 'calls'`,
135
+ );
136
+
137
+ for (let d = 1; d <= maxDepth; d++) {
138
+ const nextFrontier = [];
139
+
140
+ for (const fid of frontier) {
141
+ const callees = getCallees.all(fid);
142
+
143
+ const caller = idToNode.get(fid);
144
+
145
+ for (const c of callees) {
146
+ if (noTests && isTestFile(c.file)) continue;
147
+
148
+ // Always record the message (even for visited nodes — different caller path)
149
+ fileSet.add(c.file);
150
+ messages.push({
151
+ from: caller.file,
152
+ to: c.file,
153
+ label: c.name,
154
+ type: 'call',
155
+ depth: d,
156
+ });
157
+
158
+ if (visited.has(c.id)) continue;
159
+
160
+ visited.add(c.id);
161
+ nextFrontier.push(c.id);
162
+ idToNode.set(c.id, c);
163
+ }
164
+ }
165
+
166
+ frontier = nextFrontier;
167
+ if (frontier.length === 0) break;
168
+
169
+ if (d === maxDepth && frontier.length > 0) {
170
+ // Only mark truncated if at least one frontier node has further callees
171
+ const hasMoreCalls = frontier.some((fid) => getCallees.all(fid).length > 0);
172
+ if (hasMoreCalls) truncated = true;
173
+ }
174
+ }
175
+
176
+ // Dataflow annotations: add return arrows
177
+ if (withDataflow && messages.length > 0) {
178
+ const hasTable = db
179
+ .prepare("SELECT name FROM sqlite_master WHERE type='table' AND name='dataflow'")
180
+ .get();
181
+
182
+ if (hasTable) {
183
+ // Build name|file lookup for O(1) target node access
184
+ const nodeByNameFile = new Map();
185
+ for (const n of idToNode.values()) {
186
+ nodeByNameFile.set(`${n.name}|${n.file}`, n);
187
+ }
188
+
189
+ const getReturns = db.prepare(
190
+ `SELECT d.expression FROM dataflow d
191
+ WHERE d.source_id = ? AND d.kind = 'returns'`,
192
+ );
193
+ const getFlowsTo = db.prepare(
194
+ `SELECT d.expression FROM dataflow d
195
+ WHERE d.target_id = ? AND d.kind = 'flows_to'
196
+ ORDER BY d.param_index`,
197
+ );
198
+
199
+ // For each called function, check if it has return edges
200
+ const seenReturns = new Set();
201
+ for (const msg of [...messages]) {
202
+ if (msg.type !== 'call') continue;
203
+ const targetNode = nodeByNameFile.get(`${msg.label}|${msg.to}`);
204
+ if (!targetNode) continue;
205
+
206
+ const returnKey = `${msg.to}->${msg.from}:${msg.label}`;
207
+ if (seenReturns.has(returnKey)) continue;
208
+
209
+ const returns = getReturns.all(targetNode.id);
210
+
211
+ if (returns.length > 0) {
212
+ seenReturns.add(returnKey);
213
+ const expr = returns[0].expression || 'result';
214
+ messages.push({
215
+ from: msg.to,
216
+ to: msg.from,
217
+ label: expr,
218
+ type: 'return',
219
+ depth: msg.depth,
220
+ });
221
+ }
222
+ }
223
+
224
+ // Annotate call messages with parameter names
225
+ for (const msg of messages) {
226
+ if (msg.type !== 'call') continue;
227
+ const targetNode = nodeByNameFile.get(`${msg.label}|${msg.to}`);
228
+ if (!targetNode) continue;
229
+
230
+ const params = getFlowsTo.all(targetNode.id);
231
+
232
+ if (params.length > 0) {
233
+ const paramNames = params
234
+ .map((p) => p.expression)
235
+ .filter(Boolean)
236
+ .slice(0, 3);
237
+ if (paramNames.length > 0) {
238
+ msg.label = `${msg.label}(${paramNames.join(', ')})`;
239
+ }
240
+ }
241
+ }
242
+ }
243
+ }
244
+
245
+ // Sort messages by depth, then call before return
246
+ messages.sort((a, b) => {
247
+ if (a.depth !== b.depth) return a.depth - b.depth;
248
+ if (a.type === 'call' && b.type === 'return') return -1;
249
+ if (a.type === 'return' && b.type === 'call') return 1;
250
+ return 0;
251
+ });
252
+
253
+ // Build participant list from files
254
+ const aliases = buildAliases([...fileSet]);
255
+ const participants = [...fileSet].map((file) => ({
256
+ id: aliases.get(file),
257
+ label: file.split('/').pop(),
258
+ file,
259
+ }));
260
+
261
+ // Sort participants: entry file first, then alphabetically
262
+ participants.sort((a, b) => {
263
+ if (a.file === entry.file) return -1;
264
+ if (b.file === entry.file) return 1;
265
+ return a.file.localeCompare(b.file);
266
+ });
267
+
268
+ // Replace file paths with alias IDs in messages
269
+ for (const msg of messages) {
270
+ msg.from = aliases.get(msg.from);
271
+ msg.to = aliases.get(msg.to);
272
+ }
273
+
274
+ db.close();
275
+
276
+ const base = {
277
+ entry,
278
+ participants,
279
+ messages,
280
+ depth: maxDepth,
281
+ totalMessages: messages.length,
282
+ truncated,
283
+ };
284
+ const result = paginateResult(base, 'messages', { limit: opts.limit, offset: opts.offset });
285
+ if (opts.limit !== undefined || opts.offset !== undefined) {
286
+ const activeFiles = new Set(result.messages.flatMap((m) => [m.from, m.to]));
287
+ result.participants = result.participants.filter((p) => activeFiles.has(p.id));
288
+ }
289
+ return result;
290
+ }
291
+
292
+ // ─── Mermaid formatter ───────────────────────────────────────────────
293
+
294
+ /**
295
+ * Escape special Mermaid characters in labels.
296
+ */
297
+ function escapeMermaid(str) {
298
+ return str
299
+ .replace(/</g, '&lt;')
300
+ .replace(/>/g, '&gt;')
301
+ .replace(/:/g, '#colon;')
302
+ .replace(/"/g, '#quot;');
303
+ }
304
+
305
+ /**
306
+ * Convert sequenceData result to Mermaid sequenceDiagram syntax.
307
+ * @param {{ participants, messages, truncated }} seqResult
308
+ * @returns {string}
309
+ */
310
+ export function sequenceToMermaid(seqResult) {
311
+ const lines = ['sequenceDiagram'];
312
+
313
+ for (const p of seqResult.participants) {
314
+ lines.push(` participant ${p.id} as ${escapeMermaid(p.label)}`);
315
+ }
316
+
317
+ for (const msg of seqResult.messages) {
318
+ const arrow = msg.type === 'return' ? '-->>' : '->>';
319
+ lines.push(` ${msg.from}${arrow}${msg.to}: ${escapeMermaid(msg.label)}`);
320
+ }
321
+
322
+ if (seqResult.truncated && seqResult.participants.length > 0) {
323
+ lines.push(
324
+ ` note right of ${seqResult.participants[0].id}: Truncated at depth ${seqResult.depth}`,
325
+ );
326
+ }
327
+
328
+ return lines.join('\n');
329
+ }
330
+
331
+ // ─── CLI formatter ───────────────────────────────────────────────────
332
+
333
+ /**
334
+ * CLI entry point — format sequence data as mermaid, JSON, or ndjson.
335
+ */
336
+ export function sequence(name, dbPath, opts = {}) {
337
+ const data = sequenceData(name, dbPath, opts);
338
+
339
+ if (opts.ndjson) {
340
+ printNdjson(data, 'messages');
341
+ return;
342
+ }
343
+
344
+ if (opts.json) {
345
+ console.log(JSON.stringify(data, null, 2));
346
+ return;
347
+ }
348
+
349
+ // Default: mermaid format
350
+ if (!data.entry) {
351
+ console.log(`No matching function found for "${name}".`);
352
+ return;
353
+ }
354
+
355
+ const e = data.entry;
356
+ console.log(`\nSequence from: [${kindIcon(e.kind)}] ${e.name} ${e.file}:${e.line}`);
357
+ console.log(`Participants: ${data.participants.length} Messages: ${data.totalMessages}`);
358
+ if (data.truncated) {
359
+ console.log(` (truncated at depth ${data.depth})`);
360
+ }
361
+ console.log();
362
+
363
+ if (data.messages.length === 0) {
364
+ console.log(' (leaf node — no callees)');
365
+ return;
366
+ }
367
+
368
+ console.log(sequenceToMermaid(data));
369
+ }