@nahisaho/musubix-codegraph 2.3.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.
Files changed (66) hide show
  1. package/README.md +187 -0
  2. package/dist/codegraph.d.ts +218 -0
  3. package/dist/codegraph.d.ts.map +1 -0
  4. package/dist/codegraph.js +429 -0
  5. package/dist/codegraph.js.map +1 -0
  6. package/dist/events/event-emitter.d.ts +93 -0
  7. package/dist/events/event-emitter.d.ts.map +1 -0
  8. package/dist/events/event-emitter.js +152 -0
  9. package/dist/events/event-emitter.js.map +1 -0
  10. package/dist/events/index.d.ts +8 -0
  11. package/dist/events/index.d.ts.map +1 -0
  12. package/dist/events/index.js +8 -0
  13. package/dist/events/index.js.map +1 -0
  14. package/dist/graph/graph-engine.d.ts +97 -0
  15. package/dist/graph/graph-engine.d.ts.map +1 -0
  16. package/dist/graph/graph-engine.js +341 -0
  17. package/dist/graph/graph-engine.js.map +1 -0
  18. package/dist/graph/index.d.ts +8 -0
  19. package/dist/graph/index.d.ts.map +1 -0
  20. package/dist/graph/index.js +8 -0
  21. package/dist/graph/index.js.map +1 -0
  22. package/dist/graphrag/graphrag-search.d.ts +67 -0
  23. package/dist/graphrag/graphrag-search.d.ts.map +1 -0
  24. package/dist/graphrag/graphrag-search.js +297 -0
  25. package/dist/graphrag/graphrag-search.js.map +1 -0
  26. package/dist/graphrag/index.d.ts +8 -0
  27. package/dist/graphrag/index.d.ts.map +1 -0
  28. package/dist/graphrag/index.js +8 -0
  29. package/dist/graphrag/index.js.map +1 -0
  30. package/dist/index.d.ts +18 -0
  31. package/dist/index.d.ts.map +1 -0
  32. package/dist/index.js +20 -0
  33. package/dist/index.js.map +1 -0
  34. package/dist/indexer/index.d.ts +8 -0
  35. package/dist/indexer/index.d.ts.map +1 -0
  36. package/dist/indexer/index.js +8 -0
  37. package/dist/indexer/index.js.map +1 -0
  38. package/dist/indexer/indexer.d.ts +58 -0
  39. package/dist/indexer/indexer.d.ts.map +1 -0
  40. package/dist/indexer/indexer.js +206 -0
  41. package/dist/indexer/indexer.js.map +1 -0
  42. package/dist/parser/ast-parser.d.ts +79 -0
  43. package/dist/parser/ast-parser.d.ts.map +1 -0
  44. package/dist/parser/ast-parser.js +489 -0
  45. package/dist/parser/ast-parser.js.map +1 -0
  46. package/dist/parser/index.d.ts +8 -0
  47. package/dist/parser/index.d.ts.map +1 -0
  48. package/dist/parser/index.js +8 -0
  49. package/dist/parser/index.js.map +1 -0
  50. package/dist/storage/index.d.ts +9 -0
  51. package/dist/storage/index.d.ts.map +1 -0
  52. package/dist/storage/index.js +10 -0
  53. package/dist/storage/index.js.map +1 -0
  54. package/dist/storage/memory-storage.d.ts +102 -0
  55. package/dist/storage/memory-storage.d.ts.map +1 -0
  56. package/dist/storage/memory-storage.js +263 -0
  57. package/dist/storage/memory-storage.js.map +1 -0
  58. package/dist/storage/sqlite-storage.d.ts +90 -0
  59. package/dist/storage/sqlite-storage.d.ts.map +1 -0
  60. package/dist/storage/sqlite-storage.js +344 -0
  61. package/dist/storage/sqlite-storage.js.map +1 -0
  62. package/dist/types.d.ts +539 -0
  63. package/dist/types.d.ts.map +1 -0
  64. package/dist/types.js +99 -0
  65. package/dist/types.js.map +1 -0
  66. package/package.json +85 -0
package/README.md ADDED
@@ -0,0 +1,187 @@
1
+ # @nahisaho/musubix-codegraph
2
+
3
+ **Multi-language Code Graph Analysis Engine for MUSUBIX**
4
+
5
+ [![npm version](https://img.shields.io/npm/v/@nahisaho/musubix-codegraph.svg)](https://www.npmjs.com/package/@nahisaho/musubix-codegraph)
6
+ [![License: MIT](https://img.shields.io/badge/License-MIT-yellow.svg)](https://opensource.org/licenses/MIT)
7
+
8
+ ## Overview
9
+
10
+ `@nahisaho/musubix-codegraph` is a high-performance code graph analysis library that provides:
11
+
12
+ - **AST Parsing**: Tree-sitter based multi-language code parsing (16 languages)
13
+ - **Graph Engine**: Code entity and relationship graph construction
14
+ - **GraphRAG**: Graph-based Retrieval Augmented Generation for code search
15
+ - **YATA Integration**: Seamless integration with YATA knowledge graph
16
+
17
+ ## Installation
18
+
19
+ ```bash
20
+ npm install @nahisaho/musubix-codegraph
21
+ ```
22
+
23
+ ## Quick Start
24
+
25
+ ### Direct Library Usage
26
+
27
+ ```typescript
28
+ import { CodeGraph } from '@nahisaho/musubix-codegraph';
29
+
30
+ // Create instance
31
+ const codeGraph = new CodeGraph({
32
+ storage: 'memory', // or 'sqlite', 'yata'
33
+ });
34
+
35
+ // Index a repository
36
+ const result = await codeGraph.index('/path/to/repo');
37
+ console.log(`Indexed ${result.entitiesIndexed} entities`);
38
+
39
+ // Query the graph
40
+ const deps = await codeGraph.findDependencies('UserService', { depth: 3 });
41
+ const callers = await codeGraph.findCallers('authenticate');
42
+
43
+ // GraphRAG search
44
+ const results = await codeGraph.globalSearch('authentication flow');
45
+
46
+ // Get statistics
47
+ const stats = await codeGraph.getStats();
48
+ ```
49
+
50
+ ### Using Individual Components
51
+
52
+ ```typescript
53
+ import { ASTParser, GraphEngine, GraphRAGSearch } from '@nahisaho/musubix-codegraph';
54
+
55
+ // Parse a single file
56
+ const parser = new ASTParser();
57
+ const parseResult = await parser.parseFile('src/index.ts');
58
+
59
+ // Use graph engine directly
60
+ const graph = new GraphEngine(storage);
61
+ await graph.addEntity(entity);
62
+ const path = await graph.findPath('A', 'B');
63
+
64
+ // GraphRAG search
65
+ const search = new GraphRAGSearch(graph);
66
+ const communities = await search.detectCommunities();
67
+ ```
68
+
69
+ ## Supported Languages
70
+
71
+ | Language | Extensions | Status |
72
+ |----------|-----------|--------|
73
+ | TypeScript | `.ts`, `.tsx` | ✅ |
74
+ | JavaScript | `.js`, `.jsx`, `.mjs` | ✅ |
75
+ | Python | `.py` | ✅ |
76
+ | Rust | `.rs` | ✅ |
77
+ | Go | `.go` | ✅ |
78
+ | Java | `.java` | ✅ |
79
+ | PHP | `.php` | ✅ |
80
+ | C# | `.cs` | ✅ |
81
+ | C | `.c`, `.h` | ✅ |
82
+ | C++ | `.cpp`, `.hpp`, `.cc` | ✅ |
83
+ | Ruby | `.rb` | ✅ |
84
+ | HCL | `.tf`, `.hcl` | ✅ |
85
+
86
+ ## API Reference
87
+
88
+ ### CodeGraph (Facade)
89
+
90
+ ```typescript
91
+ class CodeGraph {
92
+ // Indexing
93
+ index(path: string): Promise<IndexResult>;
94
+ reindex(path: string): Promise<IndexResult>;
95
+
96
+ // Querying
97
+ query(query: string): Promise<QueryResult>;
98
+ findDependencies(entity: string, options?: DepOptions): Promise<Entity[]>;
99
+ findCallers(entity: string): Promise<CallPath[]>;
100
+ findCallees(entity: string): Promise<CallPath[]>;
101
+ findImplementations(interfaceName: string): Promise<Entity[]>;
102
+ analyzeModule(path: string): Promise<ModuleAnalysis>;
103
+
104
+ // Code Retrieval
105
+ getSnippet(entityId: string): Promise<CodeSnippet>;
106
+ getFileStructure(path: string): Promise<FileStructure>;
107
+
108
+ // GraphRAG
109
+ globalSearch(query: string): Promise<SearchResult[]>;
110
+ localSearch(entity: string, options?: LocalSearchOptions): Promise<SearchResult[]>;
111
+
112
+ // Statistics
113
+ getStats(): Promise<GraphStatistics>;
114
+
115
+ // Events
116
+ on(event: 'indexing:start' | 'indexing:progress' | 'indexing:complete', handler): void;
117
+ }
118
+ ```
119
+
120
+ ### Storage Options
121
+
122
+ ```typescript
123
+ const codeGraph = new CodeGraph({
124
+ storage: 'memory', // In-memory (default, fast, no persistence)
125
+ });
126
+
127
+ const codeGraph = new CodeGraph({
128
+ storage: 'sqlite', // SQLite (persistent)
129
+ sqlitePath: '.codegraph/index.db',
130
+ });
131
+
132
+ // YATA integration
133
+ import { YataDatabase } from '@nahisaho/yata-local';
134
+ import { createYataAdapter } from '@nahisaho/musubix-codegraph/storage';
135
+
136
+ const yata = new YataDatabase();
137
+ await yata.open();
138
+
139
+ const codeGraph = new CodeGraph({
140
+ storage: createYataAdapter(yata),
141
+ });
142
+ ```
143
+
144
+ ## CLI Usage
145
+
146
+ When used with MUSUBIX CLI:
147
+
148
+ ```bash
149
+ # Index repository
150
+ musubix codegraph index /path/to/repo
151
+
152
+ # Query graph
153
+ musubix codegraph query "UserService"
154
+
155
+ # Find dependencies
156
+ musubix codegraph find-deps UserService --depth 3
157
+
158
+ # Show statistics
159
+ musubix codegraph stats
160
+
161
+ # Start MCP server
162
+ musubix codegraph serve
163
+ ```
164
+
165
+ ## Performance
166
+
167
+ | Metric | Value |
168
+ |--------|-------|
169
+ | Indexing speed | ~32 entities/sec |
170
+ | Query response | < 500ms |
171
+ | Incremental index | < 2 sec |
172
+ | Memory usage | < 500MB |
173
+
174
+ ## Requirements
175
+
176
+ - Node.js >= 20.0.0
177
+ - npm >= 10.0.0
178
+
179
+ ## License
180
+
181
+ MIT License - see [LICENSE](../../LICENSE)
182
+
183
+ ## Related
184
+
185
+ - [MUSUBIX](https://github.com/nahisaho/MUSUBIX) - Neuro-Symbolic AI Integration System
186
+ - [@nahisaho/yata-local](../yata-local) - Local Knowledge Graph Storage
187
+ - [CodeGraphMCPServer](https://github.com/nahisaho/CodeGraphMCPServer) - Original Python implementation
@@ -0,0 +1,218 @@
1
+ /**
2
+ * @nahisaho/musubix-codegraph - CodeGraph Facade
3
+ *
4
+ * Main entry point for code graph analysis
5
+ *
6
+ * @packageDocumentation
7
+ * @module @nahisaho/musubix-codegraph
8
+ *
9
+ * @see REQ-CG-API-001
10
+ * @see REQ-CG-API-002
11
+ * @see REQ-CG-API-003
12
+ * @see DES-CG-001
13
+ */
14
+ import type { CodeGraphOptions, Entity, QueryResult, IndexResult, GraphStatistics, CallPath, ModuleAnalysis, CodeSnippet, FileStructure, SearchResult, GraphQuery, DependencyOptions, LocalSearchOptions, GlobalSearchOptions } from './types.js';
15
+ import { CodeGraphEventEmitter } from './events/index.js';
16
+ /**
17
+ * CodeGraph - Main facade for code graph analysis
18
+ *
19
+ * Provides a unified API for:
20
+ * - AST parsing and entity extraction
21
+ * - Graph construction and querying
22
+ * - Dependency and call graph analysis
23
+ * - GraphRAG-based semantic search
24
+ *
25
+ * @example
26
+ * ```typescript
27
+ * // Basic usage
28
+ * const codeGraph = new CodeGraph({ storage: 'memory' });
29
+ * await codeGraph.index('/path/to/repo');
30
+ *
31
+ * const deps = await codeGraph.findDependencies('UserService');
32
+ * const callers = await codeGraph.findCallers('authenticate');
33
+ * const results = await codeGraph.globalSearch('authentication');
34
+ * ```
35
+ *
36
+ * @see REQ-CG-API-001 - Library as standalone
37
+ * @see REQ-CG-API-002 - Programmatic API
38
+ */
39
+ export declare class CodeGraph extends CodeGraphEventEmitter {
40
+ private options;
41
+ private storage;
42
+ private initialized;
43
+ /**
44
+ * Create a new CodeGraph instance
45
+ *
46
+ * @param options - Configuration options
47
+ */
48
+ constructor(options?: CodeGraphOptions);
49
+ /**
50
+ * Register a handler for indexing start events
51
+ */
52
+ onIndexingStart(handler: (path: string) => void): this;
53
+ /**
54
+ * Register a handler for indexing complete events
55
+ */
56
+ onIndexingComplete(handler: (result: {
57
+ entitiesIndexed: number;
58
+ relationsIndexed: number;
59
+ errors: unknown[];
60
+ }) => void): this;
61
+ /**
62
+ * Register a handler for indexing progress events
63
+ */
64
+ onIndexingProgress(handler: (processed: number, total: number, currentFile: string) => void): this;
65
+ /**
66
+ * Register a handler for indexing error events
67
+ */
68
+ onIndexingError(handler: (error: Error, file?: string) => void): this;
69
+ /**
70
+ * Register a handler for query start events
71
+ */
72
+ onQueryStart(handler: (query: GraphQuery) => void): this;
73
+ /**
74
+ * Register a handler for query complete events
75
+ */
76
+ onQueryComplete(handler: (result: QueryResult, durationMs: number) => void): this;
77
+ /**
78
+ * Initialize the CodeGraph instance
79
+ *
80
+ * @internal
81
+ */
82
+ private ensureInitialized;
83
+ /**
84
+ * Create storage adapter based on options
85
+ *
86
+ * @internal
87
+ */
88
+ private createStorage;
89
+ /**
90
+ * Index a repository or directory
91
+ *
92
+ * @param path - Path to repository or directory
93
+ * @returns Index result with statistics
94
+ *
95
+ * @see REQ-CG-IDX-001
96
+ *
97
+ * @example
98
+ * ```typescript
99
+ * const result = await codeGraph.index('/path/to/repo');
100
+ * console.log(`Indexed ${result.entitiesIndexed} entities`);
101
+ * ```
102
+ */
103
+ index(path: string): Promise<IndexResult>;
104
+ /**
105
+ * Reindex a repository (clear and index fresh)
106
+ *
107
+ * @param path - Path to repository or directory
108
+ * @returns Index result with statistics
109
+ */
110
+ reindex(path: string): Promise<IndexResult>;
111
+ /**
112
+ * Query the code graph
113
+ *
114
+ * @param query - Search query string or query object
115
+ * @returns Query result with matching entities
116
+ *
117
+ * @see REQ-CG-GRF-003
118
+ */
119
+ query(query: string | GraphQuery): Promise<QueryResult>;
120
+ /**
121
+ * Find dependencies of an entity
122
+ *
123
+ * @param entityName - Name or ID of the entity
124
+ * @param options - Dependency analysis options
125
+ * @returns List of dependent entities
126
+ *
127
+ * @see REQ-CG-GRF-004
128
+ */
129
+ findDependencies(entityName: string, options?: DependencyOptions): Promise<Entity[]>;
130
+ /**
131
+ * Find callers of a function or method
132
+ *
133
+ * @param entityName - Name or ID of the function/method
134
+ * @returns List of call paths
135
+ *
136
+ * @see REQ-CG-GRF-005
137
+ */
138
+ findCallers(entityName: string): Promise<CallPath[]>;
139
+ /**
140
+ * Find callees of a function or method
141
+ *
142
+ * @param entityName - Name or ID of the function/method
143
+ * @returns List of call paths
144
+ *
145
+ * @see REQ-CG-GRF-005
146
+ */
147
+ findCallees(entityName: string): Promise<CallPath[]>;
148
+ /**
149
+ * Find implementations of an interface
150
+ *
151
+ * @param interfaceName - Name or ID of the interface
152
+ * @returns List of implementing entities
153
+ */
154
+ findImplementations(interfaceName: string): Promise<Entity[]>;
155
+ /**
156
+ * Analyze module structure
157
+ *
158
+ * @param path - Path to module/file
159
+ * @returns Module analysis result
160
+ */
161
+ analyzeModule(path: string): Promise<ModuleAnalysis>;
162
+ /**
163
+ * Get source code snippet for an entity
164
+ *
165
+ * @param entityId - Entity ID
166
+ * @returns Code snippet with context
167
+ */
168
+ getSnippet(entityId: string): Promise<CodeSnippet>;
169
+ /**
170
+ * Get file structure overview
171
+ *
172
+ * @param path - File path
173
+ * @returns File structure with entities
174
+ */
175
+ getFileStructure(path: string): Promise<FileStructure>;
176
+ /**
177
+ * Global search across all code communities
178
+ *
179
+ * @param query - Search query
180
+ * @param options - Search options
181
+ * @returns Search results with relevance scores
182
+ *
183
+ * @see REQ-CG-RAG-002
184
+ */
185
+ globalSearch(query: string, options?: GlobalSearchOptions): Promise<SearchResult[]>;
186
+ /**
187
+ * Local search in entity neighborhood
188
+ *
189
+ * @param entityName - Entity name or ID
190
+ * @param options - Search options
191
+ * @returns Search results from entity neighborhood
192
+ *
193
+ * @see REQ-CG-RAG-003
194
+ */
195
+ localSearch(entityName: string, options?: LocalSearchOptions): Promise<SearchResult[]>;
196
+ /**
197
+ * Get graph statistics
198
+ *
199
+ * @returns Graph statistics
200
+ */
201
+ getStats(): Promise<GraphStatistics>;
202
+ /**
203
+ * Close the CodeGraph instance and release resources
204
+ */
205
+ close(): Promise<void>;
206
+ /**
207
+ * Check if instance is initialized
208
+ */
209
+ isInitialized(): boolean;
210
+ }
211
+ /**
212
+ * Create a new CodeGraph instance
213
+ *
214
+ * @param options - Configuration options
215
+ * @returns CodeGraph instance
216
+ */
217
+ export declare function createCodeGraph(options?: CodeGraphOptions): CodeGraph;
218
+ //# sourceMappingURL=codegraph.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"codegraph.d.ts","sourceRoot":"","sources":["../src/codegraph.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;GAYG;AAEH,OAAO,KAAK,EACV,gBAAgB,EAChB,MAAM,EACN,WAAW,EACX,WAAW,EACX,eAAe,EACf,QAAQ,EACR,cAAc,EACd,WAAW,EACX,aAAa,EACb,YAAY,EACZ,UAAU,EACV,iBAAiB,EACjB,kBAAkB,EAClB,mBAAmB,EAEpB,MAAM,YAAY,CAAC;AAEpB,OAAO,EAAE,qBAAqB,EAAE,MAAM,mBAAmB,CAAC;AAE1D;;;;;;;;;;;;;;;;;;;;;;GAsBG;AACH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