-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcli.py
More file actions
245 lines (194 loc) · 8.61 KB
/
cli.py
File metadata and controls
245 lines (194 loc) · 8.61 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
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
#!/usr/bin/env python3
"""
consequencegraph — Consequence-Aware Code Knowledge Graph CLI
Usage:
consequencegraph index <path> [--preset <preset>] [--cache <cache_path>]
consequencegraph impact <target> [--depth <n>] [--preset <preset>] [--llm <adapter>] [--cache <cache_path>]
consequencegraph watch <path> [--preset <preset>] [--cache <cache_path>]
consequencegraph stats [--cache <cache_path>]
consequencegraph nodes [--type <node_type>] [--cache <cache_path>]
consequencegraph fmt --llm <adapter> [--cache <cache_path>]
"""
import sys
import json
import argparse
import os
from core.graph import KnowledgeGraph
from core.indexer import Indexer
from core.enricher import Enricher
from core.query import QueryEngine
from output.llm_context import to_json, format_for_llm, format_impact_as_context
# ── Helpers ───────────────────────────────────────────────────────────────────
def _load_graph(cache_path: str) -> KnowledgeGraph:
graph = KnowledgeGraph()
graph.CACHE_FILE = cache_path
loaded = graph.load(cache_path)
if not loaded:
print(f"[consequencegraph] ⚠ No cache found at '{cache_path}'. Run `consequencegraph index <path>` first.",
file=sys.stderr)
return graph, loaded
def _apply_preset(graph: KnowledgeGraph, preset: str):
if preset == "neural_lam":
from presets.neural_lam import apply
apply(graph)
elif preset:
print(f"[consequencegraph] ⚠ Unknown preset '{preset}'. Available: neural_lam", file=sys.stderr)
# ── Commands ──────────────────────────────────────────────────────────────────
def cmd_index(args):
path = args.path
cache_path = args.cache or os.path.join(os.getcwd(), ".consequencegraph", "cache.json")
print(f"[consequencegraph] 🔍 Indexing: {path}")
graph = KnowledgeGraph()
graph.CACHE_FILE = cache_path
indexer = Indexer(graph, path)
indexer.index_path(path)
print(f"[consequencegraph] 🧠 Running semantic enricher...")
Enricher(graph).run()
if args.preset:
_apply_preset(graph, args.preset)
graph.save(cache_path)
stats = graph.stats()
print(f"[consequencegraph] ✅ Done. {stats['total_nodes']} nodes, {stats['total_edges']} edges.")
print(f"[consequencegraph] 💾 Cache saved to: {cache_path}")
print(json.dumps(stats, indent=2))
def cmd_impact(args):
cache_path = args.cache or os.path.join(os.getcwd(), ".consequencegraph", "cache.json")
graph, loaded = _load_graph(cache_path)
if not loaded:
sys.exit(1)
if args.preset:
_apply_preset(graph, args.preset)
depth = int(args.depth) if args.depth else None
engine = QueryEngine(graph)
result = engine.impact(args.target, depth=depth)
# LLM formatter mode (piped)
if args.llm:
print(format_for_llm(result, llm=args.llm))
elif args.human:
print(format_impact_as_context(result))
else:
# Default: JSON to stdout (pipeable)
print(to_json(result))
def cmd_watch(args):
try:
from core.watcher import Watcher
except ImportError:
print("[consequencegraph] ❌ watchdog not installed. Run: pip install watchdog", file=sys.stderr)
sys.exit(1)
path = args.path
cache_path = args.cache or os.path.join(os.getcwd(), ".consequencegraph", "cache.json")
# Load existing graph or build fresh
graph = KnowledgeGraph()
graph.CACHE_FILE = cache_path
loaded = graph.load(cache_path)
if not loaded:
print(f"[consequencegraph] 🔍 No cache found. Indexing first: {path}")
indexer = Indexer(graph, path)
indexer.index_path(path)
Enricher(graph).run()
if args.preset:
_apply_preset(graph, args.preset)
graph.save(cache_path)
print(f"[consequencegraph] ✅ Initial index done: {graph.node_count()} nodes")
watcher = Watcher(graph, path)
watcher.run_forever()
def cmd_stats(args):
cache_path = args.cache or os.path.join(os.getcwd(), ".consequencegraph", "cache.json")
graph, loaded = _load_graph(cache_path)
if not loaded:
sys.exit(1)
print(to_json(graph.stats()))
def cmd_nodes(args):
cache_path = args.cache or os.path.join(os.getcwd(), ".consequencegraph", "cache.json")
graph, loaded = _load_graph(cache_path)
if not loaded:
sys.exit(1)
nodes = []
for nid, d in graph.g.nodes(data=True):
if args.type and d.get("node_type") != args.type:
continue
nodes.append({
"id": nid,
"type": d.get("node_type"),
"file": d.get("file_path"),
"line": d.get("line_no"),
})
nodes.sort(key=lambda x: x["id"])
print(to_json(nodes))
def cmd_fmt(args):
"""Read JSON impact from stdin, output formatted LLM context."""
data = sys.stdin.read()
try:
impact = json.loads(data)
except json.JSONDecodeError as e:
print(f"[consequencegraph] ❌ Invalid JSON on stdin: {e}", file=sys.stderr)
sys.exit(1)
print(format_for_llm(impact, llm=args.llm or "generic"))
# ── CLI parser ────────────────────────────────────────────────────────────────
def build_parser():
parser = argparse.ArgumentParser(
prog="consequencegraph",
description="Consequence-Aware Code Knowledge Graph — impact analysis for Python codebases.",
formatter_class=argparse.RawDescriptionHelpFormatter,
epilog="""
Examples:
consequencegraph index ./neural-lam --preset neural_lam
consequencegraph impact GraphLAM.forward
consequencegraph impact WeatherDataset.__getitem__ --depth 3
consequencegraph impact ARModel.training_step --depth auto --llm claude
consequencegraph impact training_step | consequencegraph fmt --llm openai
consequencegraph watch ./neural-lam --preset neural_lam
consequencegraph stats
consequencegraph nodes --type function
""",
)
parser.add_argument("--cache", default=None,
help="Path to cache file (default: .consequencegraph/cache.json in CWD)")
sub = parser.add_subparsers(dest="command", required=True)
# index
p_index = sub.add_parser("index", help="Index a Python codebase")
p_index.add_argument("path", help="Path to directory or file to index")
p_index.add_argument("--preset", default=None, choices=["neural_lam"],
help="Apply a domain-specific preset after indexing")
# impact
p_impact = sub.add_parser("impact", help="Get impact report for a node")
p_impact.add_argument("target", help="Function/class name or full node ID")
p_impact.add_argument("--depth", default=None,
help="Search depth (integer or 'auto'). Default: auto")
p_impact.add_argument("--preset", default=None, choices=["neural_lam"],
help="Apply domain preset before querying")
p_impact.add_argument("--llm", default=None,
choices=["claude", "openai", "ollama", "generic"],
help="Format output for a specific LLM")
p_impact.add_argument("--human", action="store_true",
help="Human-readable text output instead of JSON")
# watch
p_watch = sub.add_parser("watch", help="Watch a directory for changes and re-index live")
p_watch.add_argument("path", help="Path to watch")
p_watch.add_argument("--preset", default=None, choices=["neural_lam"])
# stats
sub.add_parser("stats", help="Show graph statistics")
# nodes
p_nodes = sub.add_parser("nodes", help="List all nodes in the graph")
p_nodes.add_argument("--type", default=None,
help="Filter by node type (function, class, module, config_key, ...)")
# fmt
p_fmt = sub.add_parser("fmt", help="Format JSON impact (from stdin) for an LLM")
p_fmt.add_argument("--llm", default="generic",
choices=["claude", "openai", "ollama", "generic"],
help="Target LLM adapter")
return parser
def main():
parser = build_parser()
args = parser.parse_args()
dispatch = {
"index": cmd_index,
"impact": cmd_impact,
"watch": cmd_watch,
"stats": cmd_stats,
"nodes": cmd_nodes,
"fmt": cmd_fmt,
}
dispatch[args.command](args)
if __name__ == "__main__":
main()