Import an existing PageIndex Cloud document into the KB by ``doc_id``. Fetches structure + page content from the cloud (no local PDF), compiles concepts, and registers a raw-less ``pageindex_cloud`` entry. Idempotent: re-importing the same ``doc_id`` is skipped. The user's cloud corpus
(doc_id: str, kb_dir: Path)
| 635 | |
| 636 | |
| 637 | def import_from_pageindex_cloud(doc_id: str, kb_dir: Path) -> Literal["added", "skipped", "failed"]: |
| 638 | """Import an existing PageIndex Cloud document into the KB by ``doc_id``. |
| 639 | |
| 640 | Fetches structure + page content from the cloud (no local PDF), compiles |
| 641 | concepts, and registers a raw-less ``pageindex_cloud`` entry. Idempotent: |
| 642 | re-importing the same ``doc_id`` is skipped. The user's cloud corpus is |
| 643 | never modified. |
| 644 | """ |
| 645 | import hashlib |
| 646 | from openkb.state import HashRegistry |
| 647 | |
| 648 | logger = logging.getLogger(__name__) |
| 649 | openkb_dir = kb_dir / ".openkb" |
| 650 | config = load_config(openkb_dir / "config.yaml") |
| 651 | _setup_llm_key(kb_dir) |
| 652 | model: str = config.get("model", DEFAULT_CONFIG["model"]) |
| 653 | |
| 654 | path_key = f"pageindex-cloud:{doc_id}" |
| 655 | synthetic_hash = hashlib.sha256(path_key.encode("utf-8")).hexdigest() |
| 656 | |
| 657 | registry = HashRegistry(openkb_dir / "hashes.json") |
| 658 | if registry.is_known(synthetic_hash): |
| 659 | click.echo(f" [SKIP] Already imported from PageIndex Cloud: {doc_id}") |
| 660 | return "skipped" |
| 661 | |
| 662 | click.echo(f"Importing from PageIndex Cloud: {doc_id}") |
| 663 | snapshot: MutationSnapshot | None = None |
| 664 | doc_name = "" |
| 665 | try: |
| 666 | try: |
| 667 | cloud = prepare_cloud_import(doc_id, kb_dir, path_key) |
| 668 | except Exception as exc: |
| 669 | click.echo(f" [ERROR] Import failed: {exc}") |
| 670 | logger.debug("Cloud import traceback:", exc_info=True) |
| 671 | return "failed" |
| 672 | |
| 673 | doc_name = cloud.doc_name |
| 674 | snapshot = snapshot_paths( |
| 675 | kb_dir, |
| 676 | _snapshot_add_paths(kb_dir, doc_name, None, None), |
| 677 | operation="cloud_import", |
| 678 | details={"doc_id": doc_id, "doc_name": doc_name}, |
| 679 | # Cloud import reads from PageIndex Cloud and writes no local blob, |
| 680 | # so .openkb/files is never touched — nothing to snapshot there. |
| 681 | hardlink_dirs={ |
| 682 | kb_dir / "wiki" / "concepts", |
| 683 | kb_dir / "wiki" / "entities", |
| 684 | }, |
| 685 | ) |
| 686 | summary_path = _write_long_doc_artifacts( |
| 687 | cloud.tree, |
| 688 | cloud.all_pages, |
| 689 | doc_name, |
| 690 | doc_id, |
| 691 | kb_dir, |
| 692 | description=cloud.description, |
| 693 | ) |
| 694 | _run_compile_with_retry( |