File size: 12,910 Bytes
ab2a1e7 22c45fc ab2a1e7 70e736e ab2a1e7 22c45fc ab2a1e7 22c45fc ab2a1e7 22c45fc ab2a1e7 70e736e ab2a1e7 70e736e ab2a1e7 22c45fc ab2a1e7 70e736e ab2a1e7 70e736e ab2a1e7 70e736e ab2a1e7 70e736e 22c45fc ab2a1e7 70e736e 22c45fc 70e736e ab2a1e7 70e736e ab2a1e7 70e736e 22c45fc ab2a1e7 70e736e ab2a1e7 70e736e ab2a1e7 70e736e 22c45fc ab2a1e7 70e736e ab2a1e7 70e736e ab2a1e7 70e736e ab2a1e7 70e736e ab2a1e7 70e736e ab2a1e7 70e736e ab2a1e7 22c45fc ab2a1e7 70e736e ab2a1e7 22c45fc ab2a1e7 22c45fc ab2a1e7 70e736e ab2a1e7 22c45fc 70e736e ab2a1e7 22c45fc ab2a1e7 22c45fc ab2a1e7 22c45fc 70e736e 22c45fc 70e736e 22c45fc ab2a1e7 22c45fc 70e736e 22c45fc ab2a1e7 70e736e 22c45fc 70e736e ab2a1e7 70e736e 22c45fc 70e736e 22c45fc 70e736e 22c45fc ab2a1e7 22c45fc ab2a1e7 22c45fc ab2a1e7 70e736e 22c45fc 70e736e 22c45fc 70e736e 22c45fc 70e736e 22c45fc 70e736e ab2a1e7 | 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 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 | """
Map builder — clean call graph with full NL explanations per node and edge.
Python: ast module (accurate).
Other languages: LLM fallback.
"""
import ast
import json
from dataclasses import dataclass, field
from typing import List, Dict, Optional
from groq import Groq
from app.config import GROQ_API_KEY, MODEL_NAME, MAX_TOKENS
NOISE_BUILTINS = {
"print", "len", "range", "append", "extend", "str", "int", "float",
"list", "dict", "set", "tuple", "isinstance", "hasattr", "getattr",
"setattr", "enumerate", "zip", "map", "filter", "sorted", "reversed",
"open", "format", "repr", "type", "super", "staticmethod", "classmethod",
"property", "encode", "decode", "strip", "split", "join", "lower",
"upper", "replace", "get", "keys", "values", "items", "update", "pop",
"insert", "remove", "index", "count", "copy", "clear", "write", "read",
"close", "next", "iter", "vars", "dir", "id", "hash", "abs", "round",
}
@dataclass
class GraphNode:
id: str
label: str
kind: str
file: str
line: int
description: str = ""
explanation: str = ""
@dataclass
class GraphEdge:
source: str
target: str
kind: str
relationship: str = ""
@dataclass
class CallGraph:
nodes: List[GraphNode] = field(default_factory=list)
edges: List[GraphEdge] = field(default_factory=list)
narrative: str = ""
errors: List[str] = field(default_factory=list)
def to_dict(self) -> dict:
return {
"nodes": [n.__dict__ for n in self.nodes],
"edges": [e.__dict__ for e in self.edges],
"narrative": self.narrative,
"errors": self.errors,
}
class _PythonVisitor(ast.NodeVisitor):
def __init__(self, filename: str, code: str):
self.filename = filename
self.code = code
self.nodes: List[GraphNode] = []
self.edges: List[GraphEdge] = []
self._scope_stack: List[str] = []
self._defined: set = set()
self._called_targets: set = set()
def _id(self, name: str) -> str:
return f"{self.filename}::{name}"
def _scope(self) -> Optional[str]:
return self._scope_stack[-1] if self._scope_stack else None
def visit_FunctionDef(self, node):
name = node.name
nid = self._id(name)
self._defined.add(name)
doc = ast.get_docstring(node) or ""
self.nodes.append(GraphNode(
id=nid, label=name, kind="function",
file=self.filename, line=node.lineno,
description=doc[:120],
))
parent = self._scope()
if parent:
edge = GraphEdge(source=self._id(parent), target=nid, kind="calls")
if not any(e.source == edge.source and e.target == edge.target for e in self.edges):
self.edges.append(edge)
self._called_targets.add(nid)
self._scope_stack.append(name)
self.generic_visit(node)
self._scope_stack.pop()
visit_AsyncFunctionDef = visit_FunctionDef
def visit_ClassDef(self, node):
name = node.name
nid = self._id(name)
self._defined.add(name)
doc = ast.get_docstring(node) or ""
self.nodes.append(GraphNode(
id=nid, label=name, kind="class",
file=self.filename, line=node.lineno,
description=doc[:120],
))
self._scope_stack.append(name)
self.generic_visit(node)
self._scope_stack.pop()
def visit_Call(self, node):
scope = self._scope()
if not scope:
self.generic_visit(node)
return
if isinstance(node.func, ast.Name):
callee = node.func.id
elif isinstance(node.func, ast.Attribute):
callee = node.func.attr
else:
self.generic_visit(node)
return
if callee in NOISE_BUILTINS or callee.startswith("__"):
self.generic_visit(node)
return
target_id = self._id(callee)
self._called_targets.add(target_id)
if callee not in self._defined:
if not any(n.id == target_id for n in self.nodes):
self.nodes.append(GraphNode(
id=target_id, label=callee, kind="external",
file=self.filename, line=getattr(node, "lineno", 0),
))
edge = GraphEdge(source=self._id(scope), target=target_id, kind="calls")
if not any(e.source == edge.source and e.target == edge.target for e in self.edges):
self.edges.append(edge)
self.generic_visit(node)
def resolve_entry_points(self):
all_targets = {e.target for e in self.edges}
for node in self.nodes:
if node.kind == "function" and node.id not in all_targets:
node.kind = "entry"
def _analyze_python(filename: str, code: str) -> CallGraph:
graph = CallGraph()
try:
tree = ast.parse(code)
visitor = _PythonVisitor(filename, code)
visitor.visit(tree)
visitor.resolve_entry_points()
graph.nodes = visitor.nodes
graph.edges = visitor.edges
except SyntaxError as e:
graph.errors.append(f"Syntax error in {filename}: {e}")
return graph
def _analyze_llm_fallback(filename: str, code: str, language: str) -> CallGraph:
graph = CallGraph()
try:
client = Groq(api_key=GROQ_API_KEY)
prompt = f"""Analyze this {language} code. Return ONLY valid JSON:
{{"nodes":[{{"id":"uid","label":"name","kind":"function|class|entry|external","line":1,"description":"one sentence"}}],"edges":[{{"source":"id1","target":"id2","kind":"calls"}}]}}
Only functions/classes. No branches/loops/exceptions. Skip trivial calls.
File: {filename}
Code:
{code[:3000]}
JSON only:"""
resp = client.chat.completions.create(
model=MODEL_NAME,
messages=[{"role": "user", "content": prompt}],
max_tokens=MAX_TOKENS, temperature=0.1,
)
text = resp.choices[0].message.content.strip()
s, e = text.find("{"), text.rfind("}") + 1
if s >= 0 and e > s:
data = json.loads(text[s:e])
for n in data.get("nodes", []):
graph.nodes.append(GraphNode(
id=f"{filename}::{n.get('id','')}",
label=n.get("label", ""),
kind=n.get("kind", "function"),
file=filename, line=n.get("line", 0),
description=n.get("description", ""),
))
for e_ in data.get("edges", []):
graph.edges.append(GraphEdge(
source=f"{filename}::{e_.get('source','')}",
target=f"{filename}::{e_.get('target','')}",
kind=e_.get("kind", "calls"),
))
except Exception as ex:
graph.errors.append(f"LLM fallback error for {filename}: {ex}")
return graph
def _generate_node_explanations(nodes: List[GraphNode], files: dict) -> List[GraphNode]:
targets = [n for n in nodes if n.kind in ("function", "class", "entry") and not n.explanation]
if not targets:
return nodes
snippets = []
for n in targets[:25]:
code_text, _ = files.get(n.file, ("", ""))
lines = code_text.split("\n")
start = max(0, n.line - 1)
snippet = "\n".join(lines[start:start + 12])
snippets.append(f"### {n.label} ({n.kind})\n```\n{snippet}\n```")
try:
client = Groq(api_key=GROQ_API_KEY)
prompt = f"""For each function/class below, return a JSON array (same order) where each object has:
- "short": one sentence max 10 words, what it does
- "full": 2-3 sentences — what it does in detail, what inputs it takes, what it returns, what breaks if it fails
Return ONLY a valid JSON array. No markdown.
{chr(10).join(f'{i+1}. {s}' for i, s in enumerate(snippets))}
JSON array:"""
resp = client.chat.completions.create(
model=MODEL_NAME,
messages=[{"role": "user", "content": prompt}],
max_tokens=1500, temperature=0.2,
)
text = resp.choices[0].message.content.strip()
s = text.find("[")
e = text.rfind("]") + 1
if s >= 0 and e > s:
results = json.loads(text[s:e])
for i, n in enumerate(targets):
if i < len(results):
n.description = results[i].get("short", n.description)
n.explanation = results[i].get("full", "")
except Exception:
pass
return nodes
def _generate_edge_relationships(edges: List[GraphEdge], nodes: List[GraphNode], files: dict) -> List[GraphEdge]:
node_map = {n.id: n for n in nodes}
meaningful = [
e for e in edges
if e.kind == "calls"
and node_map.get(e.source) and node_map.get(e.source).kind != "external"
and node_map.get(e.target) and node_map.get(e.target).kind != "external"
and not e.relationship
][:30]
if not meaningful:
return edges
pairs = []
for e in meaningful:
src = node_map.get(e.source)
tgt = node_map.get(e.target)
src_desc = (src.description or src.label) if src else e.source.split("::")[-1]
tgt_desc = (tgt.description or tgt.label) if tgt else e.target.split("::")[-1]
pairs.append(f'"{src_desc}" calls "{tgt_desc}"')
try:
client = Groq(api_key=GROQ_API_KEY)
prompt = f"""For each function call relationship, write a SHORT phrase (5-8 words) describing what they do together.
Focus on data/purpose flow. Examples: "passes user input for validation", "delegates file parsing to", "triggers cleanup after error".
Return ONLY a JSON array of strings in same order. No markdown.
{chr(10).join(f'{i+1}. {p}' for i, p in enumerate(pairs))}
JSON array:"""
resp = client.chat.completions.create(
model=MODEL_NAME,
messages=[{"role": "user", "content": prompt}],
max_tokens=800, temperature=0.3,
)
text = resp.choices[0].message.content.strip()
s = text.find("[")
e_idx = text.rfind("]") + 1
if s >= 0 and e_idx > s:
results = json.loads(text[s:e_idx])
for i, edge in enumerate(meaningful):
if i < len(results):
edge.relationship = results[i]
except Exception:
pass
return edges
def _generate_narrative(graph: CallGraph) -> str:
try:
client = Groq(api_key=GROQ_API_KEY)
entries = [n for n in graph.nodes if n.kind == "entry"]
funcs = [n for n in graph.nodes if n.kind in ("function", "class")]
edge_summary = "\n".join(
f" {e.source.split('::')[-1]} → {e.target.split('::')[-1]}"
+ (f" ({e.relationship})" if e.relationship else "")
for e in graph.edges[:40] if e.kind == "calls"
)
prompt = f"""Write a plain-English narrative (4-6 sentences) for this codebase:
- What starts execution and why
- Main flow through the code
- Key dependencies and handoffs
- What the system achieves overall
Entry points: {', '.join(n.label + (f' ({n.description})' if n.description else '') for n in entries[:8])}
Functions: {', '.join(n.label + (f' ({n.description})' if n.description else '') for n in funcs[:20])}
Flow:
{edge_summary}
Narrative:"""
resp = client.chat.completions.create(
model=MODEL_NAME,
messages=[{"role": "user", "content": prompt}],
max_tokens=400, temperature=0.3,
)
return resp.choices[0].message.content.strip()
except Exception as e:
return f"Could not generate narrative: {e}"
def build_graph(files: dict) -> CallGraph:
from app.modules.language_detector import detect_language
combined = CallGraph()
for filename, (code, language) in files.items():
if language == "python":
g = _analyze_python(filename, code)
else:
g = _analyze_llm_fallback(filename, code, language)
combined.nodes.extend(g.nodes)
combined.edges.extend(g.edges)
combined.errors.extend(g.errors)
seen = set()
deduped = []
for n in combined.nodes:
if n.id not in seen:
seen.add(n.id)
deduped.append(n)
combined.nodes = deduped
defined_ids = {n.id for n in combined.nodes if n.kind != "external"}
has_incoming = set()
for e in combined.edges:
if e.source in defined_ids:
has_incoming.add(e.target)
combined.nodes = [
n for n in combined.nodes
if n.kind != "external" or n.id in has_incoming
]
combined.nodes = _generate_node_explanations(combined.nodes, files)
combined.edges = _generate_edge_relationships(combined.edges, combined.nodes, files)
combined.narrative = _generate_narrative(combined)
return combined |