Repository navigation
Expand file tree
/
Copy pathmain.py
More file actions
151 lines (122 loc) · 5.21 KB
/
Copy pathmain.py
File metadata and controls
151 lines (122 loc) · 5.21 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
import os
import glob
import sys
from dotenv import load_dotenv
from backend.app.services.ast_parser import CodeASTParser
from backend.app.services.graph_store import Neo4jGraphStore
from backend.app.services.vector_store import CodeVectorStore
from backend.app.services.agent_workflow import CodeGraphAgent
load_dotenv()
SUPPORTED_EXTENSIONS = ["py", "java", "js", "jsx", "ts", "tsx", "cpp", "hpp", "c", "h", "go"]
def ingest_codebase(vector_store: CodeVectorStore):
"""Parses all polyglot modules and updates Neo4j and Qdrant with fresh vector embeddings."""
print("🚀 Ingesting Codebase into Neo4j & Qdrant...\n")
try:
vector_store.reset_collection()
except Exception as e:
print(f"⚠️ Vector collection reset note: {e}\n")
module_files = []
for ext in SUPPORTED_EXTENSIONS:
module_files.extend(glob.glob(f"backend/app/**/*.{ext}", recursive=True))
if not module_files:
print("❌ No matching polyglot source files found in 'backend/app/' directory.")
return
neo4j_uri = os.getenv("NEO4J_URI", "bolt://localhost:7687")
neo4j_user = os.getenv("NEO4J_USER", os.getenv("NEO4J_USERNAME", "neo4j"))
neo4j_pass = os.getenv("NEO4J_PASSWORD")
graph_store = None
if neo4j_uri and neo4j_pass:
try:
graph_store = Neo4jGraphStore(neo4j_uri, neo4j_user, neo4j_pass)
except Exception as e:
print(f"⚠️ Warning: Neo4j connection failed: {e}")
doc_id_counter = 1
total_indexed = 0
for filepath in module_files:
if "__init__.py" in filepath:
continue
parser = CodeASTParser(filepath)
ast_data = parser.extract_structures()
fn_count = len(ast_data.get("functions", []))
cls_count = len(ast_data.get("classes", []))
const_count = len(ast_data.get("constants", []))
calls_count = len(ast_data.get("calls", []))
print(f"📦 Processing '{filepath}' -> Extracted: {cls_count} classes, {fn_count} functions, {calls_count} call sites")
if graph_store:
try:
graph_store.ingest_ast(filepath, ast_data)
except Exception as e:
print(f" ❌ Graph Error: {e}")
for fn in ast_data.get("functions", []):
snippet_text = (
f"File: {filepath}\n"
f"Function/Method: {fn['name']}\n"
f"Docstring: {fn.get('docstring', '')}\n"
f"Code:\n{fn.get('code', '')}"
)
if vector_store.upsert_code_snippet(
doc_id=doc_id_counter,
code_snippet=snippet_text,
metadata={"file": filepath, "type": fn.get("type", "function"), "name": fn["name"]},
):
total_indexed += 1
doc_id_counter += 1
for cls in ast_data.get("classes", []):
method_names = [m["name"] if isinstance(m, dict) else str(m) for m in cls.get("methods", [])]
snippet_text = (
f"File: {filepath}\n"
f"Class: {cls['name']}\n"
f"Extends: {', '.join(cls.get('extends', []))}\n"
f"Methods: {', '.join(method_names)}\n"
f"Docstring: {cls.get('docstring', '')}"
)
if vector_store.upsert_code_snippet(
doc_id=doc_id_counter,
code_snippet=snippet_text,
metadata={"file": filepath, "type": "class", "name": cls["name"]},
):
total_indexed += 1
doc_id_counter += 1
for const in ast_data.get("constants", []):
snippet_text = (
f"File: {filepath}\n"
f"Constant: {const['name']}\n"
f"Docstring: {const.get('docstring', '')}\n"
f"Code:\n{const.get('code', '')}"
)
if vector_store.upsert_code_snippet(
doc_id=doc_id_counter,
code_snippet=snippet_text,
metadata={"file": filepath, "type": "constant", "name": const["name"]},
):
total_indexed += 1
doc_id_counter += 1
if graph_store:
graph_store.close()
print(f"\n🎉 Knowledge Graph & Vector Store successfully updated! (Total snippets indexed: {total_indexed})\n")
def interactive_chat():
vector_store = CodeVectorStore()
ingest_codebase(vector_store)
agent = CodeGraphAgent(vector_store=vector_store)
print("🤖 CodeGraph Agent initialized! Type 'exit' or 'quit' to stop.\n")
print("=" * 60)
try:
while True:
user_query = input("\n❓ Ask CodeGraph Agent: ").strip()
if not user_query:
continue
if user_query.lower() in ["exit", "quit"]:
print("\n👋 Exiting CodeGraph Agent. Goodbye!")
break
print("\n🔍 Analyzing codebase and querying graph + vector stores...")
answer = agent.query(user_query)
print("\n💡 Agent Response:")
print("-" * 60)
print(answer)
print("-" * 60)
except KeyboardInterrupt:
print("\n\n👋 Session interrupted. Goodbye!")
finally:
agent.close()
if __name__ == "__main__":
interactive_chat()