forked from d-cloud/d-wiki
78 lines
3.1 KiB
Python
78 lines
3.1 KiB
Python
from __future__ import annotations
|
|
|
|
import argparse
|
|
import json
|
|
import sys
|
|
from pathlib import Path
|
|
from typing import Any
|
|
|
|
from .config import DWikiConfig, load_config
|
|
from .compile import compile_kb
|
|
from .dreams import dream_kb
|
|
from .search import get_page, index_payload, search, status_payload
|
|
|
|
|
|
def main(argv: list[str] | None = None) -> int:
|
|
parser = argparse.ArgumentParser(prog="d-wiki")
|
|
parser.add_argument("--config", type=Path, default=None)
|
|
subparsers = parser.add_subparsers(dest="command", required=True)
|
|
|
|
status_parser = subparsers.add_parser("status")
|
|
status_parser.add_argument("--format", choices=["json"], default="json")
|
|
|
|
index_parser = subparsers.add_parser("index")
|
|
index_parser.add_argument("--kb", required=True)
|
|
index_parser.add_argument("--format", choices=["json"], default="json")
|
|
|
|
compile_parser = subparsers.add_parser("compile")
|
|
compile_parser.add_argument("--kb", required=True)
|
|
compile_parser.add_argument("--force", action="store_true")
|
|
compile_parser.add_argument("--llm", choices=["off", "auto", "require"], default="off")
|
|
compile_parser.add_argument("--llm-scope", choices=["queries", "all"], default="queries")
|
|
compile_parser.add_argument("--format", choices=["json"], default="json")
|
|
|
|
dream_parser = subparsers.add_parser("dream")
|
|
dream_parser.add_argument("--kb", required=True)
|
|
dream_parser.add_argument("--force", action="store_true")
|
|
dream_parser.add_argument("--format", choices=["json"], default="json")
|
|
|
|
search_parser = subparsers.add_parser("search")
|
|
search_parser.add_argument("--kb", required=True)
|
|
search_parser.add_argument("--query", required=True)
|
|
search_parser.add_argument("--max-results", type=int, default=5)
|
|
search_parser.add_argument("--format", choices=["json"], default="json")
|
|
|
|
get_parser = subparsers.add_parser("get")
|
|
get_parser.add_argument("--kb", required=True)
|
|
get_parser.add_argument("--id", required=True)
|
|
get_parser.add_argument("--format", choices=["json"], default="json")
|
|
|
|
args = parser.parse_args(argv)
|
|
try:
|
|
config = load_config(args.config)
|
|
payload = dispatch(args, config)
|
|
except Exception as exc:
|
|
print(json.dumps({"status": "error", "error": str(exc)}, ensure_ascii=False), file=sys.stderr)
|
|
return 2
|
|
print(json.dumps(payload, ensure_ascii=False, indent=2, sort_keys=True))
|
|
return 0
|
|
|
|
|
|
def dispatch(args: argparse.Namespace, config: DWikiConfig) -> dict[str, Any]:
|
|
if args.command == "status":
|
|
return status_payload(config.path, config.knowledge_bases)
|
|
kb = config.knowledge_bases.get(args.kb)
|
|
if kb is None:
|
|
raise ValueError(f"D_WIKI_KB_NOT_FOUND: {args.kb}")
|
|
if args.command == "index":
|
|
return index_payload(kb)
|
|
if args.command == "compile":
|
|
return compile_kb(kb, force=args.force, llm_mode=args.llm, llm_scope=args.llm_scope)
|
|
if args.command == "dream":
|
|
return dream_kb(kb, force=args.force)
|
|
if args.command == "search":
|
|
return search(kb, args.query, args.max_results)
|
|
if args.command == "get":
|
|
return get_page(kb, args.id)
|
|
raise ValueError(f"D_WIKI_UNKNOWN_COMMAND: {args.command}")
|