code-graph-builder 0.2.0__py3-none-any.whl

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 (93) hide show
  1. code_graph_builder/__init__.py +82 -0
  2. code_graph_builder/builder.py +366 -0
  3. code_graph_builder/cgb_cli.py +32 -0
  4. code_graph_builder/cli.py +564 -0
  5. code_graph_builder/commands_cli.py +1288 -0
  6. code_graph_builder/config.py +340 -0
  7. code_graph_builder/constants.py +708 -0
  8. code_graph_builder/embeddings/__init__.py +40 -0
  9. code_graph_builder/embeddings/qwen3_embedder.py +573 -0
  10. code_graph_builder/embeddings/vector_store.py +584 -0
  11. code_graph_builder/examples/__init__.py +0 -0
  12. code_graph_builder/examples/example_configuration.py +276 -0
  13. code_graph_builder/examples/example_kuzu_usage.py +109 -0
  14. code_graph_builder/examples/example_semantic_search_full.py +347 -0
  15. code_graph_builder/examples/generate_wiki.py +915 -0
  16. code_graph_builder/examples/graph_export_example.py +100 -0
  17. code_graph_builder/examples/rag_example.py +206 -0
  18. code_graph_builder/examples/test_cli_demo.py +129 -0
  19. code_graph_builder/examples/test_embedding_api.py +153 -0
  20. code_graph_builder/examples/test_kuzu_local.py +190 -0
  21. code_graph_builder/examples/test_rag_redis.py +390 -0
  22. code_graph_builder/graph_updater.py +605 -0
  23. code_graph_builder/guidance/__init__.py +1 -0
  24. code_graph_builder/guidance/agent.py +123 -0
  25. code_graph_builder/guidance/prompts.py +74 -0
  26. code_graph_builder/guidance/toolset.py +264 -0
  27. code_graph_builder/language_spec.py +536 -0
  28. code_graph_builder/mcp/__init__.py +21 -0
  29. code_graph_builder/mcp/api_doc_generator.py +764 -0
  30. code_graph_builder/mcp/file_editor.py +207 -0
  31. code_graph_builder/mcp/pipeline.py +777 -0
  32. code_graph_builder/mcp/server.py +161 -0
  33. code_graph_builder/mcp/tools.py +1800 -0
  34. code_graph_builder/models.py +115 -0
  35. code_graph_builder/parser_loader.py +344 -0
  36. code_graph_builder/parsers/__init__.py +7 -0
  37. code_graph_builder/parsers/call_processor.py +306 -0
  38. code_graph_builder/parsers/call_resolver.py +139 -0
  39. code_graph_builder/parsers/definition_processor.py +796 -0
  40. code_graph_builder/parsers/factory.py +119 -0
  41. code_graph_builder/parsers/import_processor.py +293 -0
  42. code_graph_builder/parsers/structure_processor.py +145 -0
  43. code_graph_builder/parsers/type_inference.py +143 -0
  44. code_graph_builder/parsers/utils.py +134 -0
  45. code_graph_builder/rag/__init__.py +68 -0
  46. code_graph_builder/rag/camel_agent.py +429 -0
  47. code_graph_builder/rag/client.py +298 -0
  48. code_graph_builder/rag/config.py +239 -0
  49. code_graph_builder/rag/cypher_generator.py +67 -0
  50. code_graph_builder/rag/llm_backend.py +210 -0
  51. code_graph_builder/rag/markdown_generator.py +352 -0
  52. code_graph_builder/rag/prompt_templates.py +440 -0
  53. code_graph_builder/rag/rag_engine.py +640 -0
  54. code_graph_builder/rag/review_report.md +172 -0
  55. code_graph_builder/rag/tests/__init__.py +3 -0
  56. code_graph_builder/rag/tests/test_camel_agent.py +313 -0
  57. code_graph_builder/rag/tests/test_client.py +221 -0
  58. code_graph_builder/rag/tests/test_config.py +177 -0
  59. code_graph_builder/rag/tests/test_markdown_generator.py +240 -0
  60. code_graph_builder/rag/tests/test_prompt_templates.py +160 -0
  61. code_graph_builder/services/__init__.py +39 -0
  62. code_graph_builder/services/graph_service.py +465 -0
  63. code_graph_builder/services/kuzu_service.py +665 -0
  64. code_graph_builder/services/memory_service.py +171 -0
  65. code_graph_builder/settings.py +75 -0
  66. code_graph_builder/tests/ACCEPTANCE_CRITERIA_PHASE2.md +401 -0
  67. code_graph_builder/tests/__init__.py +1 -0
  68. code_graph_builder/tests/run_acceptance_check.py +378 -0
  69. code_graph_builder/tests/test_api_find.py +231 -0
  70. code_graph_builder/tests/test_api_find_integration.py +226 -0
  71. code_graph_builder/tests/test_basic.py +78 -0
  72. code_graph_builder/tests/test_c_api_extraction.py +388 -0
  73. code_graph_builder/tests/test_call_resolution_scenarios.py +504 -0
  74. code_graph_builder/tests/test_embedder.py +411 -0
  75. code_graph_builder/tests/test_integration_semantic.py +434 -0
  76. code_graph_builder/tests/test_mcp_protocol.py +298 -0
  77. code_graph_builder/tests/test_mcp_user_flow.py +190 -0
  78. code_graph_builder/tests/test_rag.py +404 -0
  79. code_graph_builder/tests/test_settings.py +135 -0
  80. code_graph_builder/tests/test_step1_graph_build.py +264 -0
  81. code_graph_builder/tests/test_step2_api_docs.py +323 -0
  82. code_graph_builder/tests/test_step3_embedding.py +278 -0
  83. code_graph_builder/tests/test_vector_store.py +552 -0
  84. code_graph_builder/tools/__init__.py +40 -0
  85. code_graph_builder/tools/graph_query.py +495 -0
  86. code_graph_builder/tools/semantic_search.py +387 -0
  87. code_graph_builder/types.py +333 -0
  88. code_graph_builder/utils/__init__.py +0 -0
  89. code_graph_builder/utils/path_utils.py +30 -0
  90. code_graph_builder-0.2.0.dist-info/METADATA +321 -0
  91. code_graph_builder-0.2.0.dist-info/RECORD +93 -0
  92. code_graph_builder-0.2.0.dist-info/WHEEL +4 -0
  93. code_graph_builder-0.2.0.dist-info/entry_points.txt +3 -0
@@ -0,0 +1,276 @@
1
+ #!/usr/bin/env python3
2
+ """Examples showing all configuration options for Code Graph Builder.
3
+
4
+ This script demonstrates various ways to configure the builder:
5
+ 1. Simple dict-based configuration (quick start)
6
+ 2. Type-safe dataclass configuration (recommended)
7
+ 3. Different backends (Kùzu, Memory, Memgraph)
8
+ 4. Scan configuration options
9
+ """
10
+
11
+ from __future__ import annotations
12
+
13
+ import sys
14
+ from pathlib import Path
15
+
16
+ sys.path.insert(0, str(Path(__file__).parent.parent.parent))
17
+
18
+ from code_graph_builder import CodeGraphBuilder
19
+ from code_graph_builder.config import (
20
+ KuzuConfig,
21
+ MemgraphConfig,
22
+ MemoryConfig,
23
+ OutputConfig,
24
+ ScanConfig,
25
+ )
26
+
27
+
28
+ def example_1_simple_dict_config():
29
+ """Example 1: Simple dict-based configuration (quickest way)."""
30
+ print("=" * 80)
31
+ print("Example 1: Simple Dict Configuration")
32
+ print("=" * 80)
33
+ print()
34
+
35
+ builder = CodeGraphBuilder(
36
+ # Required: Path to code repository
37
+ repo_path="/Users/jiaojeremy/CodeFile/tinycc",
38
+ # Required: Backend type ("kuzu", "memgraph", or "memory")
39
+ backend="kuzu",
40
+ # Optional: Backend-specific configuration as dict
41
+ backend_config={
42
+ "db_path": "./example1_graph.db", # Where to store the database
43
+ "batch_size": 1000, # Batch size for writes
44
+ },
45
+ # Optional: Scan configuration as dict
46
+ scan_config={
47
+ "exclude_patterns": {"tests", "win32", "examples"}, # Skip these
48
+ "max_file_size": 10 * 1024 * 1024, # Skip files > 10MB
49
+ },
50
+ )
51
+
52
+ print(f"Builder initialized:")
53
+ print(f" Repository: {builder.repo_path}")
54
+ print(f" Backend: {builder.backend}")
55
+ print(f" Backend config: {builder.backend_config}")
56
+ print(f" Scan config: {builder.scan_config}")
57
+ print()
58
+ return builder
59
+
60
+
61
+ def example_2_type_safe_config():
62
+ """Example 2: Type-safe dataclass configuration (recommended)."""
63
+ print("=" * 80)
64
+ print("Example 2: Type-Safe Dataclass Configuration")
65
+ print("=" * 80)
66
+ print()
67
+
68
+ # Create type-safe configurations
69
+ kuzu_config = KuzuConfig(
70
+ db_path="./example2_graph.db",
71
+ batch_size=5000, # Larger batch for better performance
72
+ read_only=False,
73
+ )
74
+
75
+ scan_config = ScanConfig(
76
+ exclude_patterns={"tests", "docs", "*.md", ".git"},
77
+ include_languages={"c", "python"}, # Only scan these languages
78
+ max_file_size=5 * 1024 * 1024, # 5MB limit
79
+ follow_symlinks=False,
80
+ )
81
+
82
+ builder = CodeGraphBuilder(
83
+ repo_path="/Users/jiaojeremy/CodeFile/tinycc",
84
+ backend="kuzu",
85
+ backend_config=kuzu_config, # Pass dataclass instead of dict
86
+ scan_config=scan_config, # Pass dataclass instead of dict
87
+ )
88
+
89
+ print(f"Builder initialized with dataclasses:")
90
+ print(f" Kùzu DB path: {kuzu_config.db_path}")
91
+ print(f" Batch size: {kuzu_config.batch_size}")
92
+ print(f" Excluded: {scan_config.exclude_patterns}")
93
+ print(f" Languages: {scan_config.include_languages}")
94
+ print()
95
+ return builder
96
+
97
+
98
+ def example_3_memory_backend():
99
+ """Example 3: Memory backend (no persistence, for testing)."""
100
+ print("=" * 80)
101
+ print("Example 3: Memory Backend (No Persistence)")
102
+ print("=" * 80)
103
+ print()
104
+
105
+ mem_config = MemoryConfig(
106
+ auto_save=True, # Auto-save to JSON on exit
107
+ save_path="./memory_export.json",
108
+ )
109
+
110
+ builder = CodeGraphBuilder(
111
+ repo_path="/Users/jiaojeremy/CodeFile/tinycc",
112
+ backend="memory",
113
+ backend_config=mem_config,
114
+ scan_config=ScanConfig(
115
+ exclude_patterns={"tests", "win32"},
116
+ ),
117
+ )
118
+
119
+ print(f"Memory builder initialized:")
120
+ print(f" Auto-save: {mem_config.auto_save}")
121
+ print(f" Save path: {mem_config.save_path}")
122
+ print()
123
+ return builder
124
+
125
+
126
+ def example_4_memgraph_backend():
127
+ """Example 4: Memgraph backend (requires Docker)."""
128
+ print("=" * 80)
129
+ print("Example 4: Memgraph Backend (Docker Required)")
130
+ print("=" * 80)
131
+ print()
132
+
133
+ memgraph_config = MemgraphConfig(
134
+ host="localhost",
135
+ port=7687,
136
+ username=None, # Set if authentication enabled
137
+ password=None,
138
+ batch_size=1000,
139
+ )
140
+
141
+ builder = CodeGraphBuilder(
142
+ repo_path="/Users/jiaojeremy/CodeFile/tinycc",
143
+ backend="memgraph",
144
+ backend_config=memgraph_config,
145
+ )
146
+
147
+ print(f"Memgraph builder initialized:")
148
+ print(f" Host: {memgraph_config.host}:{memgraph_config.port}")
149
+ print(f" Auth: {'Yes' if memgraph_config.username else 'No'}")
150
+ print()
151
+ return builder
152
+
153
+
154
+ def example_5_full_configuration():
155
+ """Example 5: Full configuration with all options."""
156
+ print("=" * 80)
157
+ print("Example 5: Full Configuration")
158
+ print("=" * 80)
159
+ print()
160
+
161
+ # Complete backend configuration
162
+ backend_config = KuzuConfig(
163
+ db_path="/tmp/full_example_graph.db",
164
+ batch_size=2000,
165
+ )
166
+
167
+ # Complete scan configuration
168
+ scan_config = ScanConfig(
169
+ exclude_patterns={
170
+ "tests", # Exclude test directories
171
+ "test_", # Exclude files starting with test_
172
+ "_test.py", # Exclude Python test files
173
+ "node_modules", # Exclude JS dependencies
174
+ ".git", # Exclude git directory
175
+ "*.min.js", # Exclude minified JS
176
+ "vendor", # Exclude vendored code
177
+ },
178
+ unignore_paths={"tests/conftest.py"}, # But keep this file
179
+ include_languages=None, # Include all supported languages
180
+ max_file_size=50 * 1024 * 1024, # 50MB max file size
181
+ follow_symlinks=False,
182
+ )
183
+
184
+ builder = CodeGraphBuilder(
185
+ repo_path="/Users/jiaojeremy/CodeFile/tinycc",
186
+ backend="kuzu",
187
+ backend_config=backend_config,
188
+ scan_config=scan_config,
189
+ )
190
+
191
+ print("Full configuration:")
192
+ print(f" Backend: Kùzu at {backend_config.db_path}")
193
+ print(f" Batch size: {backend_config.batch_size}")
194
+ print(f" Exclusions: {len(scan_config.exclude_patterns)} patterns")
195
+ print(f" Max file size: {scan_config.max_file_size / 1024 / 1024:.1f}MB")
196
+ print()
197
+
198
+ # Show how to access config values
199
+ print("Configuration summary:")
200
+ print(f" Repository path: {builder.repo_path}")
201
+ print(f" Backend type: {builder.backend}")
202
+ print(f" Database path: {builder.backend_config.get('db_path')}")
203
+ print(f" Excluded patterns: {builder.scan_config.exclude_patterns}")
204
+ print()
205
+ return builder
206
+
207
+
208
+ def example_6_backward_compatibility():
209
+ """Example 6: Backward compatible (old API still works)."""
210
+ print("=" * 80)
211
+ print("Example 6: Backward Compatible API")
212
+ print("=" * 80)
213
+ print()
214
+
215
+ # Old API (still works but deprecated)
216
+ builder = CodeGraphBuilder(
217
+ repo_path="/Users/jiaojeremy/CodeFile/tinycc",
218
+ backend="kuzu",
219
+ db_config={"db_path": "./old_api_graph.db"}, # Deprecated, use backend_config
220
+ exclude_paths=frozenset({"tests"}), # Deprecated, use scan_config
221
+ )
222
+
223
+ print("Builder created with old API (deprecated but working):")
224
+ print(f" db_config -> backend_config: {builder.backend_config}")
225
+ print(f" exclude_paths -> scan_config.exclude_patterns: {builder.scan_config.exclude_patterns}")
226
+ print()
227
+ return builder
228
+
229
+
230
+ def main():
231
+ """Run all examples."""
232
+ print("Code Graph Builder - Configuration Examples")
233
+ print("=" * 80)
234
+ print()
235
+
236
+ examples = [
237
+ ("Simple Dict Config", example_1_simple_dict_config),
238
+ ("Type-Safe Config", example_2_type_safe_config),
239
+ ("Memory Backend", example_3_memory_backend),
240
+ ("Memgraph Backend", example_4_memgraph_backend),
241
+ ("Full Configuration", example_5_full_configuration),
242
+ ("Backward Compatible", example_6_backward_compatibility),
243
+ ]
244
+
245
+ builders = []
246
+ for name, example_func in examples:
247
+ try:
248
+ builder = example_func()
249
+ builders.append((name, builder))
250
+ except Exception as e:
251
+ print(f"Error in {name}: {e}")
252
+ print()
253
+
254
+ # Summary
255
+ print("=" * 80)
256
+ print("Summary")
257
+ print("=" * 80)
258
+ print()
259
+ print("Created builders:")
260
+ for name, builder in builders:
261
+ print(f" ✓ {name}: {builder.backend} backend")
262
+ print()
263
+ print("All examples completed!")
264
+ print()
265
+ print("Quick reference:")
266
+ print(" backend='kuzu' -> Embedded database, no Docker")
267
+ print(" backend='memory' -> In-memory, no persistence")
268
+ print(" backend='memgraph' -> Full database, requires Docker")
269
+ print()
270
+ print("For more details, see:")
271
+ print(" - LOCAL_DEPLOYMENT.md")
272
+ print(" - code_graph_builder/config.py")
273
+
274
+
275
+ if __name__ == "__main__":
276
+ main()
@@ -0,0 +1,109 @@
1
+ #!/usr/bin/env python3
2
+ """Example: Using code_graph_builder with Kùzu backend (no Docker)."""
3
+
4
+ from __future__ import annotations
5
+
6
+ import json
7
+ from pathlib import Path
8
+
9
+ from code_graph_builder import CodeGraphBuilder
10
+
11
+
12
+ def main():
13
+ """Demonstrate Kùzu backend usage."""
14
+ # Example repository path (change to your repo)
15
+ repo_path = "/Users/jiaojeremy/CodeFile/tinycc"
16
+
17
+ print("Code Graph Builder - Kùzu Backend Example")
18
+ print("=" * 60)
19
+ print()
20
+
21
+ # Step 1: Initialize builder with Kùzu backend
22
+ print("1. Initializing CodeGraphBuilder with Kùzu backend...")
23
+ builder = CodeGraphBuilder(
24
+ repo_path=repo_path,
25
+ backend="kuzu",
26
+ db_config={
27
+ "db_path": "./example_graph.db",
28
+ "batch_size": 1000,
29
+ },
30
+ exclude_paths=frozenset({"tests", "win32", "examples", ".git"}),
31
+ )
32
+ print(" ✅ Builder initialized")
33
+ print()
34
+
35
+ # Step 2: Build the graph
36
+ print("2. Building code graph...")
37
+ result = builder.build_graph(clean=True)
38
+ print(f" ✅ Graph built successfully")
39
+ print(f" - Nodes: {result.nodes_created}")
40
+ print(f" - Relationships: {result.relationships_created}")
41
+ print()
42
+
43
+ # Step 3: Get statistics
44
+ print("3. Getting statistics...")
45
+ stats = builder.get_statistics()
46
+ print(f" 📊 Total nodes: {stats.get('total_nodes', 0)}")
47
+ print(f" 📊 Total relationships: {stats.get('total_relationships', 0)}")
48
+ node_labels = stats.get("node_labels", {})
49
+ if node_labels:
50
+ print(f" 📊 Node labels:")
51
+ for label, count in list(node_labels.items())[:5]: # Show first 5
52
+ print(f" - {label}: {count}")
53
+ rel_types = stats.get("relationship_types", {})
54
+ if rel_types:
55
+ print(f" 📊 Relationship types:")
56
+ for rel_type, count in list(rel_types.items())[:5]: # Show first 5
57
+ print(f" - {rel_type}: {count}")
58
+ print()
59
+
60
+ # Step 4: Query the graph
61
+ print("4. Querying the graph...")
62
+ print(" Query: MATCH (f:Function) RETURN f.name LIMIT 5")
63
+ results = builder.query("MATCH (f:Function) RETURN f.name LIMIT 5")
64
+ print(f" Results ({len(results)} found):")
65
+ for i, row in enumerate(results, 1):
66
+ print(f" {i}. {row}")
67
+ print()
68
+
69
+ # Step 5: Export graph data
70
+ print("5. Exporting graph data...")
71
+ graph_data = builder.export_graph()
72
+ output_file = Path("example_export.json")
73
+ with open(output_file, "w") as f:
74
+ json.dump(graph_data, f, indent=2, default=str)
75
+ print(f" ✅ Exported to {output_file}")
76
+ print(f" - Total nodes exported: {len(graph_data.get('nodes', []))}")
77
+ print(f" - Total relationships exported: {len(graph_data.get('relationships', []))}")
78
+ print()
79
+
80
+ # Step 6: Find specific function
81
+ print("6. Finding specific function...")
82
+ func_results = builder.query(
83
+ """
84
+ MATCH (f:Function)
85
+ WHERE f.name CONTAINS 'parse'
86
+ RETURN f.name, f.qualified_name
87
+ LIMIT 3
88
+ """
89
+ )
90
+ print(f" Found {len(func_results)} functions matching 'parse':")
91
+ for row in func_results:
92
+ print(f" - {row}")
93
+ print()
94
+
95
+ print("=" * 60)
96
+ print("✅ Example completed successfully!")
97
+ print()
98
+ print("Summary:")
99
+ print(f" - Database: {Path('./example_graph.db').absolute()}")
100
+ print(f" - Export: {output_file.absolute()}")
101
+ print()
102
+ print("You can:")
103
+ print(" 1. Query the database directly using Kùzu CLI")
104
+ print(" 2. Load the database in another Python script")
105
+ print(" 3. Import the JSON export into other tools")
106
+
107
+
108
+ if __name__ == "__main__":
109
+ main()