@nexiq/extension-sdk 1.0.3 → 1.0.7

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/src/index.ts CHANGED
@@ -1,33 +1,400 @@
1
+ import type { Database } from "better-sqlite3";
2
+ import fs from "node:fs";
3
+ import path from "node:path";
1
4
  import type {
2
5
  TypeDataDeclare,
3
6
  ComponentFileVar,
4
7
  VariableName,
5
8
  DatabaseData,
9
+ GraphViewType,
10
+ WorkspacePackageRow,
11
+ PackageRow,
12
+ PackageDependencyRow,
13
+ FileRow,
14
+ ScopeRow,
15
+ EntityRow,
16
+ SymbolRow,
17
+ RenderRow,
18
+ ExportRow,
19
+ RelationRow,
20
+ VariableScope,
21
+ VariableLoc,
22
+ PropData,
23
+ TypeData,
24
+ TypeDataParam,
25
+ ComponentInfoRender,
6
26
  } from "@nexiq/shared";
7
27
 
8
28
  // Re-exporting these from shared for convenience if needed by extensions
9
- export type { TypeDataDeclare, ComponentFileVar, VariableName, DatabaseData };
29
+ export type {
30
+ TypeDataDeclare,
31
+ ComponentFileVar,
32
+ VariableName,
33
+ DatabaseData,
34
+ GraphViewType,
35
+ VariableScope,
36
+ VariableLoc,
37
+ PropData,
38
+ TypeData,
39
+ TypeDataParam,
40
+ ComponentInfoRender,
41
+ };
42
+
43
+ export interface UsageOccurrence {
44
+ usageId: string;
45
+ filePath: string;
46
+ line: number;
47
+ column: number;
48
+ ownerId: string;
49
+ ownerKind: string;
50
+ accessPath?: string[] | undefined;
51
+ isOptional?: boolean | undefined;
52
+ isComputed?: boolean | undefined;
53
+ hiddenIntermediate?: string | undefined;
54
+ displayLabel?: string | undefined;
55
+ }
56
+
57
+ export interface NodeAppearance {
58
+ color?: string | undefined;
59
+ radius?: number | undefined;
60
+ }
61
+
62
+ // Registry for custom node types registered by extensions.
63
+ const customNodeAppearances = new Map<string, NodeAppearance>();
64
+
65
+ export function registerNodeType(type: string, appearance: NodeAppearance): void {
66
+ customNodeAppearances.set(type, appearance);
67
+ }
68
+
69
+ export function getRegisteredNodeAppearance(type: string): NodeAppearance | undefined {
70
+ return customNodeAppearances.get(type);
71
+ }
72
+
73
+ export interface GraphAppearance {
74
+ nodeHighlight?: string;
75
+ comboHighlight?: string;
76
+ arrowColor?: string;
77
+ directFlowColor?: string;
78
+ sideEffectFlowColor?: string;
79
+ labelColor?: string;
80
+ gitAdded?: string;
81
+ gitModified?: string;
82
+ gitDeleted?: string;
83
+ nodes?: Partial<
84
+ Record<
85
+ | "package"
86
+ | "scope"
87
+ | "component"
88
+ | "hook"
89
+ | "renderGroup"
90
+ | "sourceGroup"
91
+ | "pathGroup"
92
+ | "callback"
93
+ | "state"
94
+ | "memo"
95
+ | "ref"
96
+ | "effect"
97
+ | "prop"
98
+ | "render"
99
+ | "variable"
100
+ | "attribute"
101
+ | "entry",
102
+ NodeAppearance
103
+ >
104
+ > & Record<string, NodeAppearance | undefined>;
105
+ typeKeyword?: string;
106
+ typeLiteral?: string;
107
+ typeString?: string;
108
+ typeNumber?: string;
109
+ typeBoolean?: string;
110
+ typePunctuation?: string;
111
+ typeReference?: string;
112
+ typeComponent?: string;
113
+ typeDefault?: string;
114
+ genericsColor?: string;
115
+ }
116
+
117
+ export type CustomColors = GraphAppearance;
118
+
119
+ export const DEFAULT_GRAPH_APPEARANCE: GraphAppearance = {
120
+ directFlowColor: "#2563eb",
121
+ sideEffectFlowColor: "#f59e0b",
122
+ gitAdded: "#22c55e",
123
+ gitModified: "#f59e0b",
124
+ gitDeleted: "#ef4444",
125
+ nodes: {
126
+ package: { color: "#0f766e", radius: 24 },
127
+ scope: { color: "#475569", radius: 18 },
128
+ component: { color: "#2563eb", radius: 20 },
129
+ hook: { color: "#7c3aed", radius: 18 },
130
+ renderGroup: { color: "#0ea5e9", radius: 18 },
131
+ sourceGroup: { color: "#14b8a6", radius: 16 },
132
+ pathGroup: { color: "#64748b", radius: 14 },
133
+ callback: { color: "#ef4444", radius: 14 },
134
+ state: { color: "#ef4444", radius: 16 },
135
+ memo: { color: "#ef4444", radius: 14 },
136
+ ref: { color: "#ef4444", radius: 14 },
137
+ effect: { color: "#eab308", radius: 14 },
138
+ prop: { color: "#22c55e", radius: 12 },
139
+ render: { color: "#2563eb", radius: 14 },
140
+ variable: { color: "#60a5fa", radius: 14 },
141
+ attribute: { color: "#f59e0b", radius: 14 },
142
+ entry: { color: "#22c55e", radius: 20 },
143
+ },
144
+ typeKeyword: "#c084fc",
145
+ typeLiteral: "#fdba74",
146
+ typeString: "#86efac",
147
+ typeNumber: "#93c5fd",
148
+ typeBoolean: "#fde047",
149
+ typePunctuation: "#6b7280",
150
+ typeReference: "#60a5fa",
151
+ typeComponent: "#67e8f9",
152
+ typeDefault: "#d1d5db",
153
+ genericsColor: "#fde047",
154
+ };
155
+
156
+ type LegacyNodeColorKeys = Partial<
157
+ Record<
158
+ | "stateNode"
159
+ | "memoNode"
160
+ | "callbackNode"
161
+ | "refNode"
162
+ | "effectNode"
163
+ | "propNode"
164
+ | "componentNode"
165
+ | "hookNode"
166
+ | "renderNode"
167
+ | "packageNode",
168
+ string
169
+ >
170
+ >;
171
+
172
+ export function normalizeGraphAppearance(
173
+ appearance?: GraphAppearance | null,
174
+ ): GraphAppearance {
175
+ const legacy = (appearance || {}) as GraphAppearance & LegacyNodeColorKeys;
176
+ const nodes = {
177
+ package: {
178
+ ...DEFAULT_GRAPH_APPEARANCE.nodes?.package,
179
+ color:
180
+ legacy.packageNode ||
181
+ appearance?.nodes?.package?.color ||
182
+ DEFAULT_GRAPH_APPEARANCE.nodes?.package?.color,
183
+ radius:
184
+ appearance?.nodes?.package?.radius ??
185
+ DEFAULT_GRAPH_APPEARANCE.nodes?.package?.radius,
186
+ },
187
+ scope: {
188
+ ...DEFAULT_GRAPH_APPEARANCE.nodes?.scope,
189
+ color:
190
+ appearance?.nodes?.scope?.color ||
191
+ DEFAULT_GRAPH_APPEARANCE.nodes?.scope?.color,
192
+ radius:
193
+ appearance?.nodes?.scope?.radius ??
194
+ DEFAULT_GRAPH_APPEARANCE.nodes?.scope?.radius,
195
+ },
196
+ component: {
197
+ ...DEFAULT_GRAPH_APPEARANCE.nodes?.component,
198
+ color:
199
+ legacy.componentNode ||
200
+ appearance?.nodes?.component?.color ||
201
+ DEFAULT_GRAPH_APPEARANCE.nodes?.component?.color,
202
+ radius:
203
+ appearance?.nodes?.component?.radius ??
204
+ DEFAULT_GRAPH_APPEARANCE.nodes?.component?.radius,
205
+ },
206
+ hook: {
207
+ ...DEFAULT_GRAPH_APPEARANCE.nodes?.hook,
208
+ color:
209
+ legacy.hookNode ||
210
+ appearance?.nodes?.hook?.color ||
211
+ DEFAULT_GRAPH_APPEARANCE.nodes?.hook?.color,
212
+ radius:
213
+ appearance?.nodes?.hook?.radius ??
214
+ DEFAULT_GRAPH_APPEARANCE.nodes?.hook?.radius,
215
+ },
216
+ renderGroup: {
217
+ ...DEFAULT_GRAPH_APPEARANCE.nodes?.renderGroup,
218
+ color:
219
+ appearance?.nodes?.renderGroup?.color ||
220
+ DEFAULT_GRAPH_APPEARANCE.nodes?.renderGroup?.color,
221
+ radius:
222
+ appearance?.nodes?.renderGroup?.radius ??
223
+ DEFAULT_GRAPH_APPEARANCE.nodes?.renderGroup?.radius,
224
+ },
225
+ sourceGroup: {
226
+ ...DEFAULT_GRAPH_APPEARANCE.nodes?.sourceGroup,
227
+ color:
228
+ appearance?.nodes?.sourceGroup?.color ||
229
+ DEFAULT_GRAPH_APPEARANCE.nodes?.sourceGroup?.color,
230
+ radius:
231
+ appearance?.nodes?.sourceGroup?.radius ??
232
+ DEFAULT_GRAPH_APPEARANCE.nodes?.sourceGroup?.radius,
233
+ },
234
+ pathGroup: {
235
+ ...DEFAULT_GRAPH_APPEARANCE.nodes?.pathGroup,
236
+ color:
237
+ appearance?.nodes?.pathGroup?.color ||
238
+ DEFAULT_GRAPH_APPEARANCE.nodes?.pathGroup?.color,
239
+ radius:
240
+ appearance?.nodes?.pathGroup?.radius ??
241
+ DEFAULT_GRAPH_APPEARANCE.nodes?.pathGroup?.radius,
242
+ },
243
+ callback: {
244
+ ...DEFAULT_GRAPH_APPEARANCE.nodes?.callback,
245
+ color:
246
+ legacy.callbackNode ||
247
+ appearance?.nodes?.callback?.color ||
248
+ DEFAULT_GRAPH_APPEARANCE.nodes?.callback?.color,
249
+ radius:
250
+ appearance?.nodes?.callback?.radius ??
251
+ DEFAULT_GRAPH_APPEARANCE.nodes?.callback?.radius,
252
+ },
253
+ state: {
254
+ ...DEFAULT_GRAPH_APPEARANCE.nodes?.state,
255
+ color:
256
+ legacy.stateNode ||
257
+ appearance?.nodes?.state?.color ||
258
+ DEFAULT_GRAPH_APPEARANCE.nodes?.state?.color,
259
+ radius:
260
+ appearance?.nodes?.state?.radius ??
261
+ DEFAULT_GRAPH_APPEARANCE.nodes?.state?.radius,
262
+ },
263
+ memo: {
264
+ ...DEFAULT_GRAPH_APPEARANCE.nodes?.memo,
265
+ color:
266
+ legacy.memoNode ||
267
+ appearance?.nodes?.memo?.color ||
268
+ DEFAULT_GRAPH_APPEARANCE.nodes?.memo?.color,
269
+ radius:
270
+ appearance?.nodes?.memo?.radius ??
271
+ DEFAULT_GRAPH_APPEARANCE.nodes?.memo?.radius,
272
+ },
273
+ ref: {
274
+ ...DEFAULT_GRAPH_APPEARANCE.nodes?.ref,
275
+ color:
276
+ legacy.refNode ||
277
+ appearance?.nodes?.ref?.color ||
278
+ DEFAULT_GRAPH_APPEARANCE.nodes?.ref?.color,
279
+ radius:
280
+ appearance?.nodes?.ref?.radius ??
281
+ DEFAULT_GRAPH_APPEARANCE.nodes?.ref?.radius,
282
+ },
283
+ effect: {
284
+ ...DEFAULT_GRAPH_APPEARANCE.nodes?.effect,
285
+ color:
286
+ legacy.effectNode ||
287
+ appearance?.nodes?.effect?.color ||
288
+ DEFAULT_GRAPH_APPEARANCE.nodes?.effect?.color,
289
+ radius:
290
+ appearance?.nodes?.effect?.radius ??
291
+ DEFAULT_GRAPH_APPEARANCE.nodes?.effect?.radius,
292
+ },
293
+ prop: {
294
+ ...DEFAULT_GRAPH_APPEARANCE.nodes?.prop,
295
+ color:
296
+ legacy.propNode ||
297
+ appearance?.nodes?.prop?.color ||
298
+ DEFAULT_GRAPH_APPEARANCE.nodes?.prop?.color,
299
+ radius:
300
+ appearance?.nodes?.prop?.radius ??
301
+ DEFAULT_GRAPH_APPEARANCE.nodes?.prop?.radius,
302
+ },
303
+ render: {
304
+ ...DEFAULT_GRAPH_APPEARANCE.nodes?.render,
305
+ color:
306
+ legacy.renderNode ||
307
+ appearance?.nodes?.render?.color ||
308
+ DEFAULT_GRAPH_APPEARANCE.nodes?.render?.color,
309
+ radius:
310
+ appearance?.nodes?.render?.radius ??
311
+ DEFAULT_GRAPH_APPEARANCE.nodes?.render?.radius,
312
+ },
313
+ variable: {
314
+ ...DEFAULT_GRAPH_APPEARANCE.nodes?.variable,
315
+ color:
316
+ appearance?.nodes?.variable?.color ||
317
+ DEFAULT_GRAPH_APPEARANCE.nodes?.variable?.color,
318
+ radius:
319
+ appearance?.nodes?.variable?.radius ??
320
+ DEFAULT_GRAPH_APPEARANCE.nodes?.variable?.radius,
321
+ },
322
+ attribute: {
323
+ ...DEFAULT_GRAPH_APPEARANCE.nodes?.attribute,
324
+ color:
325
+ appearance?.nodes?.attribute?.color ||
326
+ DEFAULT_GRAPH_APPEARANCE.nodes?.attribute?.color,
327
+ radius:
328
+ appearance?.nodes?.attribute?.radius ??
329
+ DEFAULT_GRAPH_APPEARANCE.nodes?.attribute?.radius,
330
+ },
331
+ };
332
+
333
+ return {
334
+ ...DEFAULT_GRAPH_APPEARANCE,
335
+ ...appearance,
336
+ nodes,
337
+ };
338
+ }
10
339
 
11
340
  export interface GraphItemPosition {
12
341
  x: number;
13
342
  y: number;
14
343
  }
15
344
 
345
+ export interface AppearanceOverride {
346
+ color?: string;
347
+ radius?: number;
348
+ collapsedRadius?: number;
349
+ expandedRadius?: number;
350
+ }
351
+
352
+ export interface GraphNodeDetail {
353
+ id: string;
354
+ projectPath?: string;
355
+ fileName?: string;
356
+ pureFileName?: string;
357
+ loc?: VariableLoc;
358
+ declarationKind?: "const" | "let" | "var" | "using" | "await using";
359
+ tag?: string;
360
+ componentType?: "function" | "class" | string | null;
361
+ raw?: ComponentFileVar;
362
+ scope?: VariableScope | string;
363
+ props?: PropData[];
364
+ propData?: PropData;
365
+ propType?: TypeData;
366
+ type?:
367
+ | "component"
368
+ | "hook"
369
+ | "type"
370
+ | "interface"
371
+ | "state"
372
+ | "render"
373
+ | "effect"
374
+ | "memo"
375
+ | "callback"
376
+ | "ref"
377
+ | "prop"
378
+ | "jsx"
379
+ | "normal"
380
+ | (string & {});
381
+ typeParams?: TypeDataParam[];
382
+ extends?: string[];
383
+ children?: Record<string, ComponentInfoRender>;
384
+ hooks?: string[];
385
+ gitStatus?: "added" | "modified" | "deleted";
386
+ [key: string]: unknown;
387
+ }
388
+
16
389
  export interface GraphNodeData {
17
390
  id: string;
18
391
  name: VariableName | string;
19
- label?: { text: string; fill?: string };
20
392
  type?: string;
21
- fileName?: string;
22
- pureFileName?: string;
23
- loc?: { line: number; column: number };
24
393
  radius?: number;
25
394
  color?: string;
26
395
  combo?: string;
27
396
  gitStatus?: "added" | "modified" | "deleted";
28
- declarationKind?: "const" | "let" | "var" | "using" | "await using";
29
- tag?: string;
30
- raw?: ComponentFileVar;
397
+ appearanceOverride?: AppearanceOverride;
31
398
  displayName?: string;
32
399
  [key: string]: unknown;
33
400
  }
@@ -37,6 +404,7 @@ export interface GraphComboData extends GraphNodeData {
37
404
  collapsedRadius?: number;
38
405
  expandedRadius?: number;
39
406
  padding?: number;
407
+ appearanceOverride?: AppearanceOverride;
40
408
  [key: string]: unknown;
41
409
  }
42
410
 
@@ -44,7 +412,15 @@ export interface GraphArrowData {
44
412
  id: string;
45
413
  source: string;
46
414
  target: string;
47
- label?: string;
415
+ name?: string;
416
+ edgeKind?: string;
417
+ category?: string;
418
+ flowRole?: "direct" | "side-effect" | null;
419
+ usageCount?: number;
420
+ usages?: UsageOccurrence[];
421
+ highlighted?: boolean;
422
+ dimmed?: boolean;
423
+ opensTo?: { fileName: string; line: number; column: number };
48
424
  combo?: string;
49
425
  [key: string]: unknown;
50
426
  }
@@ -57,34 +433,406 @@ export interface useGraphProps {
57
433
 
58
434
  export interface GraphViewResult extends useGraphProps {
59
435
  typeData: Record<string, TypeDataDeclare>;
436
+ details?: Record<string, GraphNodeDetail>;
437
+ }
438
+
439
+ /**
440
+ * Context provided to tasks during graph view generation.
441
+ */
442
+ export interface TaskContext {
443
+ /**
444
+ * Database instance for querying graph data.
445
+ */
446
+ db?: Database;
447
+ /**
448
+ * Root path of the project being analyzed.
449
+ */
450
+ projectRoot: string;
451
+ /**
452
+ * Specific paths within the project to focus analysis on.
453
+ */
454
+ analysisPaths?: string[];
455
+ /**
456
+ * The type of view being generated.
457
+ */
458
+ viewType: GraphViewType;
459
+ /**
460
+ * Optional pre-loaded snapshot data, for backward compatibility or when already available.
461
+ */
462
+ snapshotData?: DatabaseData;
463
+ /**
464
+ * Request-scoped cache for aggregated task data so multiple tasks do not reread SQLite.
465
+ */
466
+ taskDataCache?: DatabaseData;
467
+ /**
468
+ * Path to the original Cache Database. Native Rust tasks use this to ATTACH the database directly.
469
+ */
470
+ cacheDbPath?: string;
471
+ /**
472
+ * Optional stage profiler provided by the caller.
473
+ */
474
+ profileStage?: (
475
+ name: string,
476
+ startedAt: number,
477
+ detail?: string,
478
+ ) => void | Promise<void>;
479
+ /**
480
+ * Buffer for storing node, edge, and combo data in FlatBuffer format.
481
+ */
482
+ nodeDataBuffer?: SharedArrayBuffer;
483
+ /**
484
+ * Shared buffer for storing node details.
485
+ */
486
+ detailBuffer?: SharedArrayBuffer;
487
+ /**
488
+ * Serialized SQLite database buffer containing both input data and output tables.
489
+ */
490
+ sqliteBuffer?: Uint8Array;
60
491
  }
61
492
 
493
+ /**
494
+ * Helper to retrieve required graph data from either TaskContext.snapshotData or TaskContext.db.
495
+ * It handles workspace databases and qualifies IDs to avoid collisions.
496
+ */
497
+ export function getTaskData(context: TaskContext): DatabaseData {
498
+ const { db, snapshotData, analysisPaths, taskDataCache, projectRoot } =
499
+ context;
500
+
501
+ if (snapshotData) {
502
+ return snapshotData;
503
+ }
504
+
505
+ if (taskDataCache) {
506
+ return taskDataCache;
507
+ }
508
+
509
+ if (!db) {
510
+ const empty = {
511
+ files: [],
512
+ entities: [],
513
+ scopes: [],
514
+ symbols: [],
515
+ renders: [],
516
+ exports: [],
517
+ relations: [],
518
+ };
519
+ context.taskDataCache = empty;
520
+ return empty;
521
+ }
522
+
523
+ const existingTables = new Set(
524
+ (
525
+ db
526
+ .prepare(
527
+ "SELECT name FROM sqlite_master WHERE type IN ('table', 'view') UNION SELECT name FROM sqlite_temp_master WHERE type IN ('table', 'view')",
528
+ )
529
+ .all() as { name: string }[]
530
+ ).map((row) => row.name),
531
+ );
532
+ const tableExists = (name: string) => existingTables.has(name);
533
+
534
+ if (tableExists("workspace_packages")) {
535
+ const workspacePackages = db
536
+ .prepare("SELECT * FROM workspace_packages")
537
+ .all() as WorkspacePackageRow[];
538
+ const filteredPackages =
539
+ analysisPaths && analysisPaths.length > 0
540
+ ? workspacePackages.filter((p) => analysisPaths.includes(p.path))
541
+ : workspacePackages;
542
+
543
+ const aggregated: DatabaseData = {
544
+ packages: [],
545
+ package_dependencies: [],
546
+ files: [],
547
+ entities: [],
548
+ scopes: [],
549
+ symbols: [],
550
+ renders: [],
551
+ exports: [],
552
+ relations: [],
553
+ };
554
+
555
+ filteredPackages.forEach((pkg, index) => {
556
+ let pkgDbPath = pkg.db_path;
557
+ if (!path.isAbsolute(pkgDbPath)) {
558
+ pkgDbPath = path.resolve(projectRoot, pkgDbPath);
559
+ }
560
+
561
+ if (!fs.existsSync(pkgDbPath)) {
562
+ console.warn(
563
+ `Package database not found at ${pkgDbPath} (original: ${pkg.db_path})`,
564
+ );
565
+ return;
566
+ }
567
+
568
+ // Open individual package databases one by one to avoid SQLITE_LIMIT_ATTACHED
569
+ const DbConstructor = db.constructor as new (
570
+ path: string,
571
+ options?: { readonly?: boolean },
572
+ ) => Database;
573
+
574
+ let pkgDb: Database;
575
+ try {
576
+ pkgDb = new DbConstructor(pkgDbPath, { readonly: true });
577
+ } catch (err) {
578
+ const msg = err instanceof Error ? err.message : String(err);
579
+ throw new Error(
580
+ `Extension SDK: Failed to open package database at ${pkgDbPath}: ${msg}`,
581
+ { cause: err },
582
+ );
583
+ }
584
+
585
+ try {
586
+ const fileIdOffset = (index + 1) * 1000000;
587
+ const pkgPrefix = `workspace:${pkg.package_id}:`;
588
+ const pkgId = pkg.package_id;
589
+
590
+ const pkgExistingTables = new Set(
591
+ (
592
+ pkgDb
593
+ .prepare(
594
+ "SELECT name FROM sqlite_master WHERE type IN ('table', 'view')",
595
+ )
596
+ .all() as { name: string }[]
597
+ ).map((row) => row.name),
598
+ );
599
+ const pkgTableExists = (name: string) => pkgExistingTables.has(name);
600
+
601
+ // 1. Packages & Dependencies
602
+ const pkgRows = pkgTableExists("packages")
603
+ ? (pkgDb.prepare("SELECT * FROM packages").all() as PackageRow[])
604
+ : [];
605
+
606
+ if (pkgRows.length > 0) {
607
+ aggregated.packages!.push(
608
+ ...pkgRows.map((p) => ({
609
+ ...p,
610
+ id: pkgId, // Force ID consistency with workspace remapping
611
+ })),
612
+ );
613
+ } else {
614
+ aggregated.packages!.push({
615
+ id: pkgId,
616
+ name: pkg.name,
617
+ version: pkg.version || "0.0.0",
618
+ path: pkg.path,
619
+ });
620
+ }
621
+
622
+ if (pkgTableExists("package_dependencies")) {
623
+ aggregated.package_dependencies!.push(
624
+ ...(pkgDb
625
+ .prepare("SELECT * FROM package_dependencies")
626
+ .all() as PackageDependencyRow[]),
627
+ );
628
+ }
629
+
630
+ // 2. Files
631
+ const fRows = pkgDb.prepare("SELECT * FROM files").all() as FileRow[];
632
+ aggregated.files.push(
633
+ ...fRows.map((f) => {
634
+ let relativePath = f.path;
635
+ if (projectRoot && pkg.path) {
636
+ const absPkgPath = path.isAbsolute(pkg.path)
637
+ ? pkg.path
638
+ : path.resolve(projectRoot, pkg.path);
639
+ const absFilePath = path.join(
640
+ absPkgPath,
641
+ f.path.startsWith("/") ? f.path.slice(1) : f.path,
642
+ );
643
+ relativePath =
644
+ "/" +
645
+ path
646
+ .relative(projectRoot, absFilePath)
647
+ .replaceAll(path.sep, "/");
648
+ }
649
+ return {
650
+ ...f,
651
+ id: f.id + fileIdOffset,
652
+ package_id: f.package_id || pkgId,
653
+ path: relativePath,
654
+ };
655
+ }),
656
+ );
657
+
658
+ // 3. Scopes
659
+ const sRows = pkgDb
660
+ .prepare(
661
+ `SELECT
662
+ '${pkgPrefix}' || id as id,
663
+ file_id + ${fileIdOffset} as file_id,
664
+ CASE WHEN parent_id IS NOT NULL THEN '${pkgPrefix}' || parent_id ELSE NULL END as parent_id,
665
+ CASE WHEN entity_id IS NOT NULL THEN '${pkgPrefix}' || entity_id ELSE NULL END as entity_id,
666
+ kind, data_json
667
+ FROM scopes`,
668
+ )
669
+ .all() as ScopeRow[];
670
+ aggregated.scopes.push(...sRows);
671
+
672
+ // 4. Entities
673
+ const eRows = pkgDb
674
+ .prepare(
675
+ `SELECT
676
+ '${pkgPrefix}' || id as id,
677
+ '${pkgPrefix}' || scope_id as scope_id,
678
+ kind, name, type, line, "column", end_line, end_column, declaration_kind, data_json
679
+ FROM entities`,
680
+ )
681
+ .all() as EntityRow[];
682
+ aggregated.entities.push(...eRows);
683
+
684
+ // 5. Symbols
685
+ const symRows = pkgDb
686
+ .prepare(
687
+ `SELECT
688
+ '${pkgPrefix}' || id as id,
689
+ '${pkgPrefix}' || entity_id as entity_id,
690
+ '${pkgPrefix}' || scope_id as scope_id,
691
+ name, path, is_alias, has_default, data_json
692
+ FROM symbols`,
693
+ )
694
+ .all() as SymbolRow[];
695
+ aggregated.symbols.push(...symRows);
696
+
697
+ // 6. Renders
698
+ const rRows = pkgDb
699
+ .prepare(
700
+ `SELECT
701
+ '${pkgPrefix}' || id as id,
702
+ file_id + ${fileIdOffset} as file_id,
703
+ '${pkgPrefix}' || parent_entity_id as parent_entity_id,
704
+ CASE WHEN parent_render_id IS NOT NULL THEN '${pkgPrefix}' || parent_render_id ELSE NULL END as parent_render_id,
705
+ CASE WHEN symbol_id IS NOT NULL THEN '${pkgPrefix}' || symbol_id ELSE NULL END as symbol_id,
706
+ tag, render_index, line, "column", kind, data_json
707
+ FROM renders`,
708
+ )
709
+ .all() as RenderRow[];
710
+ aggregated.renders.push(...rRows);
711
+
712
+ // 7. Exports
713
+ if (pkgTableExists("exports")) {
714
+ const expRows = pkgDb
715
+ .prepare(
716
+ `SELECT
717
+ '${pkgPrefix}' || id as id,
718
+ '${pkgPrefix}' || scope_id as scope_id,
719
+ CASE WHEN symbol_id IS NOT NULL THEN '${pkgPrefix}' || symbol_id ELSE NULL END as symbol_id,
720
+ CASE WHEN entity_id IS NOT NULL THEN '${pkgPrefix}' || entity_id ELSE NULL END as entity_id,
721
+ name, is_default
722
+ FROM exports`,
723
+ )
724
+ .all() as ExportRow[];
725
+ aggregated.exports.push(...expRows);
726
+ }
727
+
728
+ // 8. Relations
729
+ const relRows = pkgDb
730
+ .prepare(
731
+ `SELECT
732
+ '${pkgPrefix}' || from_id as from_id,
733
+ '${pkgPrefix}' || to_id as to_id,
734
+ kind, line, "column", data_json
735
+ FROM relations`,
736
+ )
737
+ .all() as RelationRow[];
738
+ aggregated.relations.push(...relRows);
739
+ } finally {
740
+ pkgDb.close();
741
+ }
742
+ });
743
+
744
+ context.taskDataCache = aggregated;
745
+ return {
746
+ ...aggregated,
747
+ diff: context.snapshotData?.diff || context.taskDataCache?.diff,
748
+ };
749
+ }
750
+
751
+ // Single project database fallback
752
+ const hasPackages = tableExists("packages");
753
+ const hasDeps = tableExists("package_dependencies");
754
+ const hasExports = tableExists("exports");
755
+
756
+ const singleProjectData = {
757
+ packages: hasPackages
758
+ ? (db.prepare("SELECT * FROM packages").all() as PackageRow[])
759
+ : [],
760
+ package_dependencies: hasDeps
761
+ ? (db
762
+ .prepare("SELECT * FROM package_dependencies")
763
+ .all() as PackageDependencyRow[])
764
+ : [],
765
+ files: db.prepare("SELECT * FROM files").all() as FileRow[],
766
+ entities: db.prepare("SELECT * FROM entities").all() as EntityRow[],
767
+ scopes: db.prepare("SELECT * FROM scopes").all() as ScopeRow[],
768
+ symbols: db.prepare("SELECT * FROM symbols").all() as SymbolRow[],
769
+ renders: db.prepare("SELECT * FROM renders").all() as RenderRow[],
770
+ exports: hasExports
771
+ ? (db.prepare("SELECT * FROM exports").all() as ExportRow[])
772
+ : [],
773
+ relations: db.prepare("SELECT * FROM relations").all() as RelationRow[],
774
+ diff: context.snapshotData?.diff || context.taskDataCache?.diff,
775
+ };
776
+ context.taskDataCache = singleProjectData;
777
+ return singleProjectData;
778
+ }
779
+
780
+ /**
781
+ * A task that contributes to the construction of a graph view.
782
+ */
62
783
  export interface GraphViewTask {
63
784
  id: string;
64
785
  priority: number;
65
- run: (
66
- data: DatabaseData,
67
- result: GraphViewResult,
68
- batch?: Partial<DatabaseData>,
69
- ) => GraphViewResult;
786
+ extensionPath?: string;
787
+ /**
788
+ * Run the task to update the graph result.
789
+ *
790
+ * @param result The current graph result to be updated
791
+ * @param context Context containing database and project information
792
+ * @returns Updated GraphViewResult
793
+ */
794
+ run?: (result: GraphViewResult, context: TaskContext) => GraphViewResult;
795
+ /**
796
+ * Run the task using buffers for improved performance.
797
+ *
798
+ * @param nodeData Shared buffer for node/edge/combo data
799
+ * @param detailBuffer Shared buffer for node details
800
+ * @param context Context containing database and project information
801
+ */
802
+ runBuffer?: (
803
+ nodeData: SharedArrayBuffer,
804
+ detailBuffer: SharedArrayBuffer,
805
+ context: TaskContext,
806
+ ) => number | void;
807
+ /**
808
+ * Run the task using an in-memory SQLite database.
809
+ * The database is expected to be initialized with input data and out_* tables.
810
+ */
811
+ runSqlite?: (
812
+ context: TaskContext,
813
+ ) => void | Promise<void> | Uint8Array | Promise<Uint8Array>;
70
814
  }
71
815
 
72
- export interface DetailSectionProps {
816
+ export interface DetailSectionProps<TGraph = unknown> {
73
817
  selectedId: string;
74
818
  item: GraphNodeData | GraphComboData;
75
- graph: unknown; // GraphData instance
819
+ graph: TGraph;
76
820
  projectPath: string;
77
821
  typeData: Record<string, TypeDataDeclare>;
822
+ detail?: GraphNodeDetail;
78
823
  onSelect?: (id: string) => void;
79
824
  renderNodes?: GraphNodeData[];
80
825
  }
81
826
 
82
- export interface DetailSection {
827
+ export interface DetailSection<TGraph = unknown> {
83
828
  id: string;
84
829
  title: string;
85
830
  priority: number;
86
- component: React.ComponentType<DetailSectionProps>;
87
- shouldShow: (item: GraphNodeData | GraphComboData) => boolean;
831
+ component: React.ComponentType<DetailSectionProps<TGraph>>;
832
+ shouldShow: (
833
+ item: GraphNodeData | GraphComboData,
834
+ detail?: GraphNodeDetail,
835
+ ) => boolean;
88
836
  defaultOpen?: boolean;
89
837
  }
90
838
 
@@ -101,12 +849,60 @@ export interface MCPTool {
101
849
  handler: (args: MCPToolHandlerArgs) => Promise<unknown>;
102
850
  }
103
851
 
104
- export interface Extension {
852
+ export interface Extension<TGraph = unknown> {
105
853
  id: string;
106
854
  viewTasks?: Record<string, GraphViewTask[]>; // Mapping of GraphViewType to tasks
107
- detailSections?: DetailSection[];
855
+ detailSections?: DetailSection<TGraph>[];
108
856
  mcpTools?: MCPTool[];
857
+ nodeTypes?: Record<string, NodeAppearance>; // Custom node types to register
858
+ }
859
+
860
+ /**
861
+ * Initialize standard output tables in a SQLite database for extension results.
862
+ */
863
+ export function initOutputTables(db: Database) {
864
+ db.exec(`
865
+ CREATE TABLE IF NOT EXISTS out_nodes (
866
+ id TEXT PRIMARY KEY,
867
+ name TEXT,
868
+ type TEXT,
869
+ combo_id TEXT,
870
+ color TEXT,
871
+ radius REAL,
872
+ display_name TEXT,
873
+ git_status TEXT,
874
+ meta_json TEXT
875
+ );
876
+ CREATE TABLE IF NOT EXISTS out_edges (
877
+ id TEXT PRIMARY KEY,
878
+ source TEXT,
879
+ target TEXT,
880
+ name TEXT,
881
+ kind TEXT,
882
+ category TEXT,
883
+ meta_json TEXT
884
+ );
885
+ CREATE TABLE IF NOT EXISTS out_combos (
886
+ id TEXT PRIMARY KEY,
887
+ name TEXT,
888
+ type TEXT,
889
+ parent_id TEXT,
890
+ color TEXT,
891
+ radius REAL,
892
+ collapsed INTEGER,
893
+ display_name TEXT,
894
+ git_status TEXT,
895
+ meta_json TEXT
896
+ );
897
+ CREATE TABLE IF NOT EXISTS out_details (
898
+ id TEXT PRIMARY KEY,
899
+ file_name TEXT,
900
+ project_path TEXT,
901
+ line INTEGER,
902
+ column INTEGER,
903
+ data_json TEXT
904
+ );
905
+ `);
109
906
  }
110
907
 
111
- export * from "./tasks/componentTask.js";
112
- export * from "./tasks/gitTask.js";
908
+ export type * from "./db.js";