|
| 1 | +"""``synaptic-snapshot`` CLI — generate a markdown summary of a graph. |
| 2 | +
|
| 3 | +Usage:: |
| 4 | +
|
| 5 | + synaptic-snapshot path/to/graph.sqlite |
| 6 | + synaptic-snapshot path/to/graph.sqlite --output report.md |
| 7 | + synaptic-snapshot path/to/graph.sqlite --max-entities 20000 |
| 8 | +
|
| 9 | +The output is the same markdown the ``knowledge_snapshot`` MCP tool and |
| 10 | +``graph.chat()``'s priming path emit. Use it to preview "what does my |
| 11 | +graph look like" without spinning up an agent. |
| 12 | +""" |
| 13 | + |
| 14 | +from __future__ import annotations |
| 15 | + |
| 16 | +import argparse |
| 17 | +import asyncio |
| 18 | +import sys |
| 19 | +from pathlib import Path |
| 20 | + |
| 21 | +from synaptic import __version__ |
| 22 | + |
| 23 | + |
| 24 | +def _build_parser() -> argparse.ArgumentParser: |
| 25 | + p = argparse.ArgumentParser( |
| 26 | + prog="synaptic-snapshot", |
| 27 | + description=( |
| 28 | + "Generate a markdown snapshot of a Synaptic Memory graph — " |
| 29 | + "scale, categories, top phrase hubs, structured tables, edge " |
| 30 | + "kinds, and sample query hints." |
| 31 | + ), |
| 32 | + ) |
| 33 | + p.add_argument("db", help="Path to the SQLite graph file (or :memory: for ephemeral)") |
| 34 | + p.add_argument( |
| 35 | + "-o", |
| 36 | + "--output", |
| 37 | + type=Path, |
| 38 | + default=None, |
| 39 | + help="Write the markdown to this file. Default: stdout.", |
| 40 | + ) |
| 41 | + p.add_argument( |
| 42 | + "--max-entities", |
| 43 | + type=int, |
| 44 | + default=5_000, |
| 45 | + help="Cap on entity scan (default 5000). Higher = more accurate phrase-hub ranking, slower.", |
| 46 | + ) |
| 47 | + p.add_argument( |
| 48 | + "--top-phrase-hubs", |
| 49 | + type=int, |
| 50 | + default=15, |
| 51 | + help="Number of phrase hubs to surface (default 15).", |
| 52 | + ) |
| 53 | + p.add_argument( |
| 54 | + "--top-categories", |
| 55 | + type=int, |
| 56 | + default=30, |
| 57 | + help="Number of categories to list (default 30).", |
| 58 | + ) |
| 59 | + p.add_argument( |
| 60 | + "--no-sample-queries", |
| 61 | + action="store_true", |
| 62 | + help="Omit the sample-queries section.", |
| 63 | + ) |
| 64 | + p.add_argument( |
| 65 | + "--title", |
| 66 | + default="Knowledge Graph Snapshot", |
| 67 | + help="H1 heading title for the report.", |
| 68 | + ) |
| 69 | + p.add_argument("--version", action="version", version=f"synaptic-snapshot {__version__}") |
| 70 | + return p |
| 71 | + |
| 72 | + |
| 73 | +async def _run(args: argparse.Namespace) -> str: |
| 74 | + from synaptic.backends.sqlite_graph import SqliteGraphBackend |
| 75 | + from synaptic.snapshot import generate_snapshot |
| 76 | + |
| 77 | + backend = SqliteGraphBackend(args.db) |
| 78 | + await backend.connect() |
| 79 | + try: |
| 80 | + return await generate_snapshot( |
| 81 | + backend, |
| 82 | + max_entities_scanned=args.max_entities, |
| 83 | + top_n_phrase_hubs=args.top_phrase_hubs, |
| 84 | + top_n_categories=args.top_categories, |
| 85 | + include_sample_queries=not args.no_sample_queries, |
| 86 | + title=args.title, |
| 87 | + ) |
| 88 | + finally: |
| 89 | + # Best-effort close — some backends raise during shutdown. |
| 90 | + close = getattr(backend, "close", None) |
| 91 | + if callable(close): |
| 92 | + try: |
| 93 | + await close() |
| 94 | + except Exception: |
| 95 | + pass |
| 96 | + |
| 97 | + |
| 98 | +def main(argv: list[str] | None = None) -> int: |
| 99 | + parser = _build_parser() |
| 100 | + args = parser.parse_args(argv) |
| 101 | + |
| 102 | + if not Path(args.db).exists() and args.db != ":memory:": |
| 103 | + print(f"error: graph file not found: {args.db}", file=sys.stderr) |
| 104 | + return 2 |
| 105 | + |
| 106 | + md = asyncio.run(_run(args)) |
| 107 | + |
| 108 | + if args.output is None: |
| 109 | + sys.stdout.write(md) |
| 110 | + else: |
| 111 | + args.output.write_text(md, encoding="utf-8") |
| 112 | + print(f"Wrote snapshot ({len(md)} chars) to {args.output}", file=sys.stderr) |
| 113 | + return 0 |
| 114 | + |
| 115 | + |
| 116 | +if __name__ == "__main__": |
| 117 | + raise SystemExit(main()) |
0 commit comments