-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsentinel_cli.py
More file actions
396 lines (318 loc) · 14 KB
/
Copy pathsentinel_cli.py
File metadata and controls
396 lines (318 loc) · 14 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
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
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
#!/usr/bin/env python
"""
Sentinel CLI - Standalone script version
This script can be run directly without installation issues.
Usage: python -m sentinel_cli <command>
"""
import asyncio
import os
import subprocess
import sys
from pathlib import Path
from typing import Optional
import typer
from dotenv import load_dotenv, set_key
from rich.console import Console
from rich.panel import Panel
from rich.progress import Progress, SpinnerColumn, TextColumn
console = Console()
app = typer.Typer(
name="sentinel",
help="Sentinel - Self-Healing Temporal Knowledge Graph",
add_completion=False,
)
def check_docker_running() -> bool:
"""Check if Docker is running."""
try:
result = subprocess.run(
["docker", "ps"],
capture_output=True,
text=True,
timeout=5
)
return result.returncode == 0
except (subprocess.TimeoutExpired, FileNotFoundError):
return False
def check_neo4j_connection(uri: str, user: str, password: str) -> bool:
"""Check if Neo4j is accessible."""
try:
from neo4j import GraphDatabase
driver = GraphDatabase.driver(uri, auth=(user, password))
driver.verify_connectivity()
driver.close()
return True
except Exception:
return False
@app.command()
def init(
force: bool = typer.Option(False, "--force", "-f", help="Overwrite existing .env file")
):
""" Interactive setup wizard for Sentinel."""
console.print(Panel.fit(
"[bold cyan] Sentinel Setup Wizard[/bold cyan]\n"
"Let's get you started in less than 5 minutes!",
border_style="cyan"
))
env_file = Path(".env")
if env_file.exists() and not force:
console.print("\n[yellow] .env file already exists![/yellow]")
overwrite = typer.confirm("Do you want to overwrite it?")
if not overwrite:
console.print("[red]Setup cancelled.[/red]")
raise typer.Exit(1)
# Step 1: Check Docker
console.print("\n[bold]Step 1: Checking Docker...[/bold]")
with Progress(
SpinnerColumn(),
TextColumn("[progress.description]{task.description}"),
console=console,
) as progress:
task = progress.add_task("Checking Docker status...", total=None)
docker_running = check_docker_running()
progress.update(task, completed=True)
if docker_running:
console.print("[green] Docker is running[/green]")
else:
console.print("[yellow] Docker is not running or not installed[/yellow]")
console.print(" You'll need Docker for Neo4j. Install from: https://docker.com")
# Step 2: Collect configuration
console.print("\n[bold]Step 2: Configuration[/bold]")
config = {}
# Neo4j settings
console.print("\n[cyan]Neo4j Configuration:[/cyan]")
config["NEO4J_URI"] = typer.prompt("Neo4j URI", default="bolt://localhost:7687")
config["NEO4J_USER"] = typer.prompt("Neo4j Username", default="neo4j")
config["NEO4J_PASSWORD"] = typer.prompt("Neo4j Password", hide_input=True)
# LLM settings
console.print("\n[cyan]LLM Configuration:[/cyan]")
console.print("Choose your LLM provider:")
console.print(" 1. Ollama (Local, Free)")
console.print(" 2. OpenAI (Cloud, Paid)")
console.print(" 3. Other (via LiteLLM)")
llm_choice = typer.prompt("Enter choice", default="1")
if llm_choice == "1":
config["OLLAMA_MODEL"] = typer.prompt("Ollama Model", default="llama3")
config["OLLAMA_BASE_URL"] = typer.prompt("Ollama URL", default="http://localhost:11434")
elif llm_choice == "2":
config["OPENAI_API_KEY"] = typer.prompt("OpenAI API Key", hide_input=True)
config["OLLAMA_MODEL"] = "gpt-4"
else:
config["OLLAMA_MODEL"] = typer.prompt("LiteLLM Model Name (e.g., claude-3-opus)")
config["LLM_API_KEY"] = typer.prompt("API Key", hide_input=True, default="")
# Scraper settings
console.print("\n[cyan]Scraper Configuration:[/cyan]")
use_firecrawl = typer.confirm("Do you have a Firecrawl API key? (optional)", default=False)
if use_firecrawl:
config["FIRECRAWL_API_KEY"] = typer.prompt("Firecrawl API Key", hide_input=True)
else:
console.print("[dim]Using local scraper (free fallback)[/dim]")
# Step 3: Write .env file
console.print("\n[bold]Step 3: Writing configuration...[/bold]")
with open(env_file, "w") as f:
f.write("# Sentinel Configuration\n")
f.write("# Generated by: sentinel init\n\n")
f.write("# Neo4j Database\n")
f.write(f"NEO4J_URI={config['NEO4J_URI']}\n")
f.write(f"NEO4J_USER={config['NEO4J_USER']}\n")
f.write(f"NEO4J_PASSWORD={config['NEO4J_PASSWORD']}\n\n")
f.write("# LLM Configuration\n")
f.write(f"OLLAMA_MODEL={config.get('OLLAMA_MODEL', 'llama3')}\n")
if "OLLAMA_BASE_URL" in config:
f.write(f"OLLAMA_BASE_URL={config['OLLAMA_BASE_URL']}\n")
if "OPENAI_API_KEY" in config:
f.write(f"OPENAI_API_KEY={config['OPENAI_API_KEY']}\n")
if "LLM_API_KEY" in config:
f.write(f"LLM_API_KEY={config['LLM_API_KEY']}\n")
f.write("\n")
if "FIRECRAWL_API_KEY" in config:
f.write("# Scraper Configuration\n")
f.write(f"FIRECRAWL_API_KEY={config['FIRECRAWL_API_KEY']}\n")
console.print("[green] Configuration saved to .env[/green]")
# Step 4: Verify connections
console.print("\n[bold]Step 4: Verifying connections...[/bold]")
load_dotenv(env_file, override=True)
with Progress(
SpinnerColumn(),
TextColumn("[progress.description]{task.description}"),
console=console,
) as progress:
task = progress.add_task("Testing Neo4j connection...", total=None)
neo4j_ok = check_neo4j_connection(
config["NEO4J_URI"],
config["NEO4J_USER"],
config["NEO4J_PASSWORD"]
)
progress.update(task, completed=True)
if neo4j_ok:
console.print("[green] Neo4j connection successful[/green]")
else:
console.print("[red] Could not connect to Neo4j[/red]")
console.print(" Make sure Neo4j is running:")
console.print(" docker run -p 7687:7687 -p 7474:7474 neo4j:latest")
# Check scraper
console.print("\n[cyan]Scraper Status:[/cyan]")
from sentinel_core.scraper import print_scraper_status
print_scraper_status()
console.print("\n" + "="*60)
console.print(Panel.fit(
"[bold green] Setup Complete![/bold green]\n\n"
"Next steps:\n"
" 1. Start Neo4j if not running\n"
" 2. Try: [cyan]python -m sentinel_cli watch https://example.com[/cyan]\n"
" 3. Try: [cyan]python -m sentinel_cli status[/cyan]\n\n"
"For help: [cyan]python -m sentinel_cli --help[/cyan]",
border_style="green"
))
@app.command()
def version():
"""Show Sentinel version."""
console.print("[cyan]Sentinel v0.1.0[/cyan]")
console.print("[dim]Self-Healing Temporal Knowledge Graph[/dim]")
@app.command()
def status():
""" Show Sentinel system status."""
load_dotenv()
from sentinel_core import GraphManager
from sentinel_core.scraper import print_scraper_status
console.print(Panel.fit(
"[bold cyan] Sentinel System Status[/bold cyan]",
border_style="cyan"
))
# Check Neo4j
console.print("\n[bold]Database Connection:[/bold]")
try:
graph_manager = GraphManager()
graph_manager.verify_connectivity()
console.print("[green] Neo4j connected[/green]")
snapshot = graph_manager.get_graph_snapshot()
console.print(f" Nodes: [cyan]{snapshot['metadata']['node_count']}[/cyan]")
console.print(f" Edges: [cyan]{snapshot['metadata']['link_count']}[/cyan]")
graph_manager.close()
except Exception as e:
console.print(f"[red] Neo4j connection failed: {str(e)}[/red]")
# Check scraper
console.print("\n[bold]Scraper Status:[/bold]")
print_scraper_status()
# Check LLM
console.print("\n[bold]LLM Configuration:[/bold]")
model = os.getenv("OLLAMA_MODEL", "Not configured")
console.print(f" Model: [cyan]{model}[/cyan]")
if "ollama" in model.lower():
base_url = os.getenv("OLLAMA_BASE_URL", "http://localhost:11434")
console.print(f" Ollama URL: [cyan]{base_url}[/cyan]")
@app.command()
def watch(
url: str = typer.Argument(..., help="URL to process"),
verbose: bool = typer.Option(False, "--verbose", "-v", help="Show detailed output")
):
""" Process a URL through the Sentinel pipeline."""
load_dotenv()
from sentinel_core import GraphManager, GraphExtractor, Sentinel
from sentinel_core.scraper import get_scraper
console.print(Panel.fit(
f"[bold cyan]Processing URL[/bold cyan]\n{url}",
border_style="cyan"
))
async def process():
try:
with Progress(
SpinnerColumn(),
TextColumn("[progress.description]{task.description}"),
console=console,
) as progress:
task = progress.add_task("Initializing Sentinel...", total=None)
graph_manager = GraphManager()
scraper = get_scraper()
extractor = GraphExtractor(
model_name=os.getenv("OLLAMA_MODEL", "ollama/llama3")
)
sentinel = Sentinel(graph_manager, scraper, extractor)
progress.update(task, completed=True)
console.print(f"[dim]Using scraper: {scraper.get_name()}[/dim]")
with Progress(
SpinnerColumn(),
TextColumn("[progress.description]{task.description}"),
console=console,
) as progress:
task = progress.add_task(f"Processing {url}...", total=None)
result = await sentinel.process_url(url)
progress.update(task, completed=True)
if result["status"] == "success":
console.print("\n[bold green] Success![/bold green]")
console.print(f" Extracted [cyan]{result.get('extracted_nodes', 0)}[/cyan] nodes")
console.print(f" Extracted [cyan]{result.get('extracted_edges', 0)}[/cyan] edges")
console.print(f" Content hash: [dim]{result.get('hash', 'N/A')[:16]}...[/dim]")
elif result["status"] == "unchanged_verified":
console.print("\n[yellow] Content unchanged[/yellow]")
console.print(f" Verified [cyan]{result.get('edges_updated', 0)}[/cyan] existing edges")
else:
console.print(f"\n[red] Error: {result.get('error', 'Unknown error')}[/red]")
if verbose and "stats" in result:
console.print(f"\n[dim]Stats: {result['stats']}[/dim]")
graph_manager.close()
except Exception as e:
console.print(f"\n[red] Error: {str(e)}[/red]")
if verbose:
import traceback
console.print(f"[dim]{traceback.format_exc()}[/dim]")
raise typer.Exit(1)
asyncio.run(process())
@app.command()
def heal(
days: int = typer.Option(7, "--days", "-d", help="Threshold for stale nodes (days)"),
dry_run: bool = typer.Option(False, "--dry-run", help="Show what would be healed without doing it")
):
""" Run a healing cycle to update stale knowledge."""
load_dotenv()
from sentinel_core import GraphManager, GraphExtractor, Sentinel
from sentinel_core.scraper import get_scraper
console.print(Panel.fit(
f"[bold cyan]Healing Cycle[/bold cyan]\n"
f"Threshold: {days} days",
border_style="cyan"
))
async def run_heal():
try:
with Progress(
SpinnerColumn(),
TextColumn("[progress.description]{task.description}"),
console=console,
) as progress:
task = progress.add_task("Initializing...", total=None)
graph_manager = GraphManager()
scraper = get_scraper()
extractor = GraphExtractor(
model_name=os.getenv("OLLAMA_MODEL", "ollama/llama3")
)
sentinel = Sentinel(graph_manager, scraper, extractor)
progress.update(task, completed=True)
console.print("\n[bold]Finding stale nodes...[/bold]")
stale_urls = graph_manager.find_stale_nodes(days_threshold=days)
if not stale_urls:
console.print("[green] No stale nodes found! Graph is healthy.[/green]")
graph_manager.close()
return
console.print(f"[yellow]Found {len(stale_urls)} stale URLs[/yellow]")
if dry_run:
console.print("\n[bold]Stale URLs (dry run):[/bold]")
for url in stale_urls[:10]:
console.print(f" {url}")
if len(stale_urls) > 10:
console.print(f" ... and {len(stale_urls) - 10} more")
graph_manager.close()
return
console.print("\n[bold]Running healing cycle...[/bold]")
result = await sentinel.run_healing_cycle(days_threshold=days)
console.print(f"\n[green] Healing complete![/green]")
console.print(f" Processed: [cyan]{result['processed_count']}[/cyan] URLs")
console.print(f" Duration: [cyan]{result['duration_seconds']:.1f}s[/cyan]")
graph_manager.close()
except Exception as e:
console.print(f"\n[red] Error: {str(e)}[/red]")
raise typer.Exit(1)
asyncio.run(run_heal())
if __name__ == "__main__":
try:
app()
except SystemExit:
pass