ltcai 8.8.0 → 9.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (111) hide show
  1. package/README.md +33 -37
  2. package/auto_setup.py +84 -70
  3. package/docs/CHANGELOG.md +113 -237
  4. package/docs/CODE_REVIEW_2026-07-06.md +764 -0
  5. package/docs/COMMUNITY_AND_PLUGINS.md +3 -3
  6. package/docs/DEVELOPMENT.md +10 -10
  7. package/docs/LEGACY_COMPATIBILITY.md +1 -1
  8. package/docs/ONBOARDING.md +2 -2
  9. package/docs/PRODUCT_DIRECTION_REVIEW.md +3 -2
  10. package/docs/ROADMAP_RECOMMENDATIONS.md +61 -36
  11. package/docs/TRUST_MODEL.md +5 -1
  12. package/docs/WHY_LATTICE.md +4 -3
  13. package/docs/architecture.md +4 -0
  14. package/docs/kg-schema.md +1 -1
  15. package/lattice_brain/__init__.py +1 -1
  16. package/lattice_brain/archive.py +4 -9
  17. package/lattice_brain/conversations.py +156 -21
  18. package/lattice_brain/embeddings.py +38 -2
  19. package/lattice_brain/graph/_kg_common.py +39 -496
  20. package/lattice_brain/graph/_kg_constants.py +243 -0
  21. package/lattice_brain/graph/_kg_fsutil.py +297 -0
  22. package/lattice_brain/graph/discovery.py +0 -948
  23. package/lattice_brain/graph/discovery_index.py +972 -0
  24. package/lattice_brain/graph/json_utils.py +25 -0
  25. package/lattice_brain/graph/retrieval.py +66 -597
  26. package/lattice_brain/graph/retrieval_docgen.py +210 -0
  27. package/lattice_brain/graph/retrieval_vector.py +460 -0
  28. package/lattice_brain/graph/runtime.py +16 -0
  29. package/lattice_brain/graph/store.py +6 -0
  30. package/lattice_brain/ingestion.py +68 -0
  31. package/lattice_brain/portability.py +1 -9
  32. package/lattice_brain/quality.py +98 -4
  33. package/lattice_brain/runtime/agent_runtime.py +166 -0
  34. package/lattice_brain/runtime/multi_agent.py +1 -1
  35. package/lattice_brain/utils.py +28 -0
  36. package/latticeai/__init__.py +1 -1
  37. package/latticeai/api/chat.py +368 -418
  38. package/latticeai/api/chat_helpers.py +227 -0
  39. package/latticeai/api/computer_use.py +149 -31
  40. package/latticeai/api/marketplace.py +11 -0
  41. package/latticeai/api/mcp.py +3 -2
  42. package/latticeai/api/models.py +4 -1
  43. package/latticeai/api/permissions.py +72 -33
  44. package/latticeai/api/setup.py +17 -2
  45. package/latticeai/api/tools.py +105 -62
  46. package/latticeai/app_factory.py +101 -296
  47. package/latticeai/core/agent.py +25 -7
  48. package/latticeai/core/io_utils.py +37 -0
  49. package/latticeai/core/legacy_compatibility.py +1 -1
  50. package/latticeai/core/local_embeddings.py +2 -4
  51. package/latticeai/core/marketplace.py +33 -2
  52. package/latticeai/core/mcp_catalog.py +450 -0
  53. package/latticeai/core/mcp_registry.py +2 -441
  54. package/latticeai/core/sessions.py +11 -3
  55. package/latticeai/core/tool_registry.py +15 -4
  56. package/latticeai/core/users.py +4 -9
  57. package/latticeai/core/workspace_os.py +1 -1
  58. package/latticeai/core/workspace_os_utils.py +3 -17
  59. package/latticeai/integrations/telegram_bot.py +7 -2
  60. package/latticeai/models/model_providers.py +111 -0
  61. package/latticeai/models/router.py +58 -136
  62. package/latticeai/runtime/audit_runtime.py +27 -16
  63. package/latticeai/runtime/automation_runtime.py +9 -0
  64. package/latticeai/runtime/bootstrap.py +1 -1
  65. package/latticeai/runtime/history_runtime.py +163 -0
  66. package/latticeai/runtime/namespace_runtime.py +173 -0
  67. package/latticeai/runtime/network_config_runtime.py +56 -0
  68. package/latticeai/runtime/sso_config_runtime.py +128 -0
  69. package/latticeai/runtime/user_key_runtime.py +106 -0
  70. package/latticeai/services/app_context.py +1 -0
  71. package/latticeai/services/architecture_readiness.py +2 -2
  72. package/latticeai/services/memory_service.py +213 -0
  73. package/latticeai/services/model_engines.py +79 -12
  74. package/latticeai/services/model_runtime.py +24 -4
  75. package/latticeai/services/platform_runtime.py +9 -1
  76. package/latticeai/services/process_audit.py +208 -0
  77. package/latticeai/services/product_readiness.py +11 -11
  78. package/latticeai/services/review_queue.py +64 -11
  79. package/latticeai/services/run_executor.py +21 -0
  80. package/latticeai/services/search_service.py +106 -30
  81. package/latticeai/services/setup_detection.py +80 -0
  82. package/latticeai/services/tool_dispatch.py +66 -0
  83. package/latticeai/services/workspace_service.py +15 -0
  84. package/package.json +1 -1
  85. package/scripts/check_i18n_literals.mjs +20 -8
  86. package/scripts/i18n_literal_allowlist.json +34 -0
  87. package/scripts/lint_frontend.mjs +6 -2
  88. package/setup_wizard.py +196 -74
  89. package/src-tauri/Cargo.lock +1 -1
  90. package/src-tauri/Cargo.toml +1 -1
  91. package/src-tauri/tauri.conf.json +1 -1
  92. package/static/app/asset-manifest.json +11 -11
  93. package/static/app/assets/{Act-C7K9wsO9.js → Act-21lIXx2E.js} +1 -1
  94. package/static/app/assets/Brain-BqUd5UJJ.js +321 -0
  95. package/static/app/assets/{Capture-B3V4_5Xp.js → Capture-BA7Z2Q1u.js} +1 -1
  96. package/static/app/assets/{Library-Cgj-EF50.js → Library-bFMtyni3.js} +1 -1
  97. package/static/app/assets/System-K6krGCqn.js +1 -0
  98. package/static/app/assets/index-C4R3ws30.js +17 -0
  99. package/static/app/assets/index-ChSeOB02.css +2 -0
  100. package/static/app/assets/primitives-sQU3it5I.js +1 -0
  101. package/static/app/assets/{textarea-CVQkN2Tk.js → textarea-DK3Fd_lR.js} +1 -1
  102. package/static/app/index.html +2 -2
  103. package/static/css/tokens.css +4 -2
  104. package/static/sw.js +1 -1
  105. package/tools/local_files.py +6 -0
  106. package/latticeai/runtime/sso_runtime.py +0 -52
  107. package/static/app/assets/Brain-I1OSzxJu.js +0 -321
  108. package/static/app/assets/System-D1Lkei3I.js +0 -1
  109. package/static/app/assets/index--P0ksosz.js +0 -17
  110. package/static/app/assets/index-DCh5AoXt.css +0 -2
  111. package/static/app/assets/primitives-BLqaKk5g.js +0 -1
@@ -505,951 +505,3 @@ class KnowledgeGraphDiscoveryMixin:
505
505
  "root_path": source["root_path"],
506
506
  "removed_graph_nodes": len(graph_node_ids),
507
507
  }
508
-
509
- def _extract_local_file_text(
510
- self, path: Path, category: str, *, include_ocr: bool
511
- ) -> Tuple[str, Dict[str, Any]]:
512
- ext = path.suffix.lower()
513
- meta: Dict[str, Any] = {"parser": _parser_type_for_category(category, ext)}
514
- text = ""
515
- if category in {"text", "code"} or ext == ".csv":
516
- text = path.read_text(encoding="utf-8", errors="replace")
517
- elif ext == ".pdf":
518
- import pdfplumber
519
-
520
- with pdfplumber.open(str(path)) as pdf:
521
- meta["pages"] = len(pdf.pages)
522
- text = "\n\n".join((page.extract_text() or "") for page in pdf.pages)
523
- elif ext == ".docx":
524
- from docx import Document
525
-
526
- doc = Document(str(path))
527
- paragraphs = [p.text for p in doc.paragraphs if p.text.strip()]
528
- table_lines = []
529
- for table in doc.tables:
530
- for row in table.rows:
531
- cells = [_clean_text(cell.text) for cell in row.cells]
532
- if any(cells):
533
- table_lines.append("\t".join(cells))
534
- meta["paragraphs"] = len(paragraphs)
535
- meta["tables"] = len(doc.tables)
536
- meta["table_rows"] = len(table_lines)
537
- text = "\n\n".join([*paragraphs, *table_lines])
538
- elif ext == ".xlsx":
539
- from openpyxl import load_workbook
540
-
541
- wb = load_workbook(str(path), read_only=True, data_only=True)
542
- rows_all = []
543
- non_empty_rows = 0
544
- non_empty_cells = 0
545
- char_count = 0
546
- for ws in wb.worksheets:
547
- sheet_rows = []
548
- for row in ws.iter_rows(values_only=True):
549
- cells = [
550
- str(cell).strip() if cell is not None else "" for cell in row
551
- ]
552
- if not any(cells):
553
- continue
554
- line = "\t".join(cells)
555
- non_empty_rows += 1
556
- non_empty_cells += sum(1 for cell in cells if cell)
557
- sheet_rows.append(line)
558
- char_count += len(line) + 1
559
- if char_count > 200_000:
560
- break
561
- if sheet_rows:
562
- rows_all.append(f"[Sheet: {ws.title}]")
563
- rows_all.extend(sheet_rows)
564
- if char_count > 200_000:
565
- break
566
- meta["sheets"] = len(wb.worksheets)
567
- meta["rows"] = non_empty_rows
568
- meta["cells"] = non_empty_cells
569
- text = "\n".join(rows_all)
570
- elif ext == ".pptx":
571
- from pptx import Presentation
572
-
573
- prs = Presentation(str(path))
574
- slides_text = []
575
- for index, slide in enumerate(prs.slides, 1):
576
- parts = []
577
- for shape in slide.shapes:
578
- if getattr(shape, "has_text_frame", False):
579
- slide_text = shape.text_frame.text.strip()
580
- if slide_text:
581
- parts.append(slide_text)
582
- if parts:
583
- slides_text.append(f"[Slide {index}]\n" + "\n".join(parts))
584
- meta["slides"] = len(prs.slides)
585
- meta["text_slides"] = len(slides_text)
586
- text = "\n\n".join(slides_text)
587
- elif category == "image":
588
- from PIL import Image
589
-
590
- with Image.open(str(path)) as image:
591
- meta.update(
592
- {
593
- "width": image.width,
594
- "height": image.height,
595
- "format": image.format,
596
- "mode": image.mode,
597
- "ocr_enabled": bool(include_ocr),
598
- }
599
- )
600
- if include_ocr:
601
- try:
602
- import pytesseract
603
-
604
- text = pytesseract.image_to_string(image)
605
- meta["ocr_chars"] = len(text)
606
- except (
607
- Exception
608
- ) as exc: # pragma: no cover - depends on local OCR runtime
609
- meta["ocr_error"] = str(exc)
610
- text = ""
611
- return text[:200_000], meta
612
-
613
- def _ensure_local_hierarchy(
614
- self,
615
- conn: sqlite3.Connection,
616
- *,
617
- source_id: str,
618
- root: Path,
619
- file_path: Path,
620
- os_type: str,
621
- drive_id: str,
622
- ) -> str:
623
- computer_label = platform.node() or "내 컴퓨터"
624
- computer_id = f"computer:{_slug(computer_label)}"
625
- drive_node_id = f"drive:{_sha256_text(f'{os_type}:{drive_id}')[:24]}"
626
- root_folder_id = f"folder:{_sha256_text(f'{source_id}:root')[:24]}"
627
- self._upsert_node(
628
- conn, computer_id, "Computer", computer_label, metadata={"os_type": os_type}
629
- )
630
- self._upsert_node(
631
- conn,
632
- drive_node_id,
633
- "Drive",
634
- drive_id,
635
- metadata={"os_type": os_type, "drive_id": drive_id},
636
- )
637
- self._upsert_edge(
638
- conn,
639
- computer_id,
640
- drive_node_id,
641
- "포함함",
642
- metadata={"source": "local_scan"},
643
- )
644
- self._upsert_node(
645
- conn,
646
- root_folder_id,
647
- "Folder",
648
- root.name or str(root),
649
- summary=str(root),
650
- metadata={"source_id": source_id, "path": str(root), "root": True},
651
- )
652
- self._upsert_edge(
653
- conn,
654
- drive_node_id,
655
- root_folder_id,
656
- "포함함",
657
- metadata={"source": "local_scan"},
658
- )
659
-
660
- try:
661
- relative_parent = file_path.parent.relative_to(root)
662
- except ValueError:
663
- relative_parent = Path()
664
- parent_id = root_folder_id
665
- current_path = root
666
- for part in relative_parent.parts:
667
- current_path = current_path / part
668
- folder_id = (
669
- f"folder:{_sha256_text(f'{source_id}:{current_path.as_posix()}')[:24]}"
670
- )
671
- self._upsert_node(
672
- conn,
673
- folder_id,
674
- "Folder",
675
- part,
676
- summary=str(current_path),
677
- metadata={
678
- "source_id": source_id,
679
- "path": str(current_path),
680
- "root": False,
681
- },
682
- )
683
- self._upsert_edge(
684
- conn, parent_id, folder_id, "포함함", metadata={"source": "local_scan"}
685
- )
686
- parent_id = folder_id
687
- return parent_id
688
-
689
- def _upsert_local_file_index(
690
- self,
691
- conn: sqlite3.Connection,
692
- *,
693
- source_id: str,
694
- root: Path,
695
- file_path: Path,
696
- stat: Optional[os.stat_result],
697
- os_type: str,
698
- drive_id: str,
699
- status: str,
700
- parser_type: str,
701
- sha256: Optional[str] = None,
702
- graph_node_id: Optional[str] = None,
703
- error_message: Optional[str] = None,
704
- metadata: Optional[Dict[str, Any]] = None,
705
- ) -> str:
706
- try:
707
- relative_path = file_path.relative_to(root).as_posix()
708
- except ValueError:
709
- relative_path = file_path.name
710
- index_id = f"local-index:{_sha256_text(f'{source_id}:{relative_path}')[:24]}"
711
- now = _now()
712
- size = stat.st_size if stat else None
713
- modified_at = _safe_iso_from_stat_mtime(stat.st_mtime) if stat else ""
714
- conn.execute(
715
- """
716
- INSERT INTO local_file_index(
717
- id, source_id, os_type, drive_id, root_path, file_path, relative_path,
718
- file_name, extension, size_bytes, modified_at, sha256, last_scanned_at,
719
- last_indexed_at, parser_type, status, error_message, graph_node_id,
720
- deleted, metadata_json
721
- )
722
- VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)
723
- ON CONFLICT(source_id, relative_path) DO UPDATE SET
724
- os_type=excluded.os_type,
725
- drive_id=excluded.drive_id,
726
- root_path=excluded.root_path,
727
- file_path=excluded.file_path,
728
- file_name=excluded.file_name,
729
- extension=excluded.extension,
730
- size_bytes=excluded.size_bytes,
731
- modified_at=excluded.modified_at,
732
- sha256=excluded.sha256,
733
- last_scanned_at=excluded.last_scanned_at,
734
- last_indexed_at=excluded.last_indexed_at,
735
- parser_type=excluded.parser_type,
736
- status=excluded.status,
737
- error_message=excluded.error_message,
738
- graph_node_id=excluded.graph_node_id,
739
- deleted=excluded.deleted,
740
- metadata_json=excluded.metadata_json
741
- """,
742
- (
743
- index_id,
744
- source_id,
745
- os_type,
746
- drive_id,
747
- str(root),
748
- str(file_path),
749
- relative_path,
750
- file_path.name,
751
- file_path.suffix.lower(),
752
- size,
753
- modified_at,
754
- sha256,
755
- now,
756
- now if status == "indexed" else None,
757
- parser_type,
758
- status,
759
- error_message,
760
- graph_node_id,
761
- 0 if status != "deleted" else 1,
762
- _json(metadata),
763
- ),
764
- )
765
- return index_id
766
-
767
- def _upsert_local_file_node(
768
- self,
769
- conn: sqlite3.Connection,
770
- *,
771
- source_id: str,
772
- root: Path,
773
- file_path: Path,
774
- stat: os.stat_result,
775
- os_type: str,
776
- drive_id: str,
777
- sha256: str,
778
- category: str,
779
- parser_type: str,
780
- text: str,
781
- parser_meta: Dict[str, Any],
782
- ) -> str:
783
- text = _clean_text(text)
784
- if not text:
785
- raise ValueError("텍스트 추출 결과가 비어 있습니다.")
786
- try:
787
- relative_path = file_path.relative_to(root).as_posix()
788
- except ValueError:
789
- relative_path = file_path.name
790
- file_node_id = f"local-file:{_sha256_text(f'{source_id}:{relative_path}')[:24]}"
791
- parent_folder_id = self._ensure_local_hierarchy(
792
- conn,
793
- source_id=source_id,
794
- root=root,
795
- file_path=file_path,
796
- os_type=os_type,
797
- drive_id=drive_id,
798
- )
799
- child_rows = conn.execute(
800
- """
801
- SELECT e.to_node AS id
802
- FROM edges e
803
- JOIN nodes n ON n.id=e.to_node
804
- WHERE e.from_node=? AND n.type IN ('Chunk', 'ImageText', 'Section')
805
- """,
806
- (file_node_id,),
807
- ).fetchall()
808
- child_ids = [row["id"] for row in child_rows]
809
- conn.execute("DELETE FROM chunks WHERE source_node=?", (file_node_id,))
810
- if child_ids:
811
- placeholders = ",".join("?" * len(child_ids))
812
- conn.execute(f"DELETE FROM nodes WHERE id IN ({placeholders})", child_ids)
813
- self._v2_delete_nodes(conn, child_ids)
814
- conn.execute("DELETE FROM edges WHERE from_node=?", (file_node_id,))
815
- self._v2_delete_edges_from(conn, file_node_id)
816
-
817
- metadata = {
818
- "source": "local_folder",
819
- "source_id": source_id,
820
- "root_path": str(root),
821
- "file_path": str(file_path),
822
- "relative_path": relative_path,
823
- "filename": file_path.name,
824
- "ext": file_path.suffix.lower(),
825
- "category": category,
826
- "parser_type": parser_type,
827
- "bytes": stat.st_size,
828
- "modified_at": _safe_iso_from_stat_mtime(stat.st_mtime),
829
- "sha256": sha256,
830
- "parser": parser_meta,
831
- }
832
- self._upsert_node(
833
- conn,
834
- file_node_id,
835
- _node_type_for_category(category),
836
- file_path.name,
837
- summary=text[:700],
838
- metadata=metadata,
839
- raw=metadata,
840
- )
841
- self._upsert_edge(
842
- conn,
843
- parent_folder_id,
844
- file_node_id,
845
- "포함함",
846
- weight=1.0,
847
- metadata={"source": "local_scan"},
848
- )
849
-
850
- target_for_concepts = text
851
- if category == "image" and text:
852
- image_text_id = f"imagetext:{_sha256_text(f'{file_node_id}:ocr')[:24]}"
853
- self._upsert_node(
854
- conn,
855
- image_text_id,
856
- "ImageText",
857
- f"{file_path.name} OCR",
858
- summary=_clean_text(text)[:700],
859
- metadata={
860
- "source_node": file_node_id,
861
- "source_id": source_id,
862
- "chars": len(text),
863
- },
864
- )
865
- self._upsert_edge(
866
- conn,
867
- file_node_id,
868
- image_text_id,
869
- "포함함",
870
- weight=0.8,
871
- metadata={"source": "ocr"},
872
- )
873
-
874
- for index, chunk in enumerate(_chunks(text)):
875
- chunk_id = f"chunk:{_sha256_text(f'{file_node_id}:{index}:{chunk}')[:24]}"
876
- self._upsert_node(
877
- conn,
878
- chunk_id,
879
- "Chunk",
880
- f"{file_path.name} chunk {index + 1}",
881
- summary=chunk[:500],
882
- metadata={
883
- "index": index,
884
- "source_node": file_node_id,
885
- "source_id": source_id,
886
- },
887
- )
888
- self._upsert_chunk(
889
- conn,
890
- chunk_id=chunk_id,
891
- source_node=file_node_id,
892
- text=chunk,
893
- metadata={
894
- "index": index,
895
- "source_node": file_node_id,
896
- "source_id": source_id,
897
- },
898
- )
899
- self._upsert_edge(
900
- conn,
901
- file_node_id,
902
- chunk_id,
903
- "포함함",
904
- weight=0.7,
905
- metadata={"source": "local_scan"},
906
- )
907
-
908
- concepts = _extract_concepts(target_for_concepts, limit=18)
909
- concept_ids: Dict[str, str] = {}
910
- for concept in concepts:
911
- node_t = _classify_node_type(concept, target_for_concepts)
912
- concept_id = f"{node_t.lower()}:{_slug(concept)}"
913
- concept_ids[concept.lower()] = concept_id
914
- self._upsert_node(
915
- conn,
916
- concept_id,
917
- node_t,
918
- concept,
919
- metadata={
920
- "auto_extracted": True,
921
- "source": "local_folder",
922
- "source_id": source_id,
923
- },
924
- )
925
- self._upsert_edge(
926
- conn,
927
- file_node_id,
928
- concept_id,
929
- "언급함",
930
- weight=0.75,
931
- metadata={"source": "local_scan"},
932
- )
933
-
934
- for triple in _extract_triples(target_for_concepts, concepts, limit=20):
935
- subj_id = concept_ids.get(triple["subject"].lower())
936
- obj_id = concept_ids.get(triple["object"].lower())
937
- if subj_id and obj_id and subj_id != obj_id:
938
- self._upsert_edge(
939
- conn,
940
- subj_id,
941
- obj_id,
942
- triple["relation"],
943
- weight=0.9,
944
- metadata={
945
- "context": triple.get("context", "")[:240],
946
- "source_id": source_id,
947
- },
948
- )
949
-
950
- for item in _semantic_items(target_for_concepts):
951
- sem_type = item["type"]
952
- sem_title = item["title"]
953
- sem_id = f"{sem_type.lower()}:{_sha256_text(f'{file_node_id}:{sem_type}:{sem_title}')[:24]}"
954
- self._upsert_node(
955
- conn,
956
- sem_id,
957
- sem_type,
958
- sem_title,
959
- summary=item["summary"],
960
- metadata={
961
- "auto_extracted": True,
962
- "source_node": file_node_id,
963
- "filename": file_path.name,
964
- },
965
- raw=item,
966
- )
967
- self._upsert_edge(conn, file_node_id, sem_id, "포함함", weight=0.9)
968
-
969
- return file_node_id
970
-
971
- def _delete_local_file_graph(
972
- self, conn: sqlite3.Connection, file_node_id: Optional[str]
973
- ) -> None:
974
- if not file_node_id:
975
- return
976
-
977
- file_row = conn.execute(
978
- "SELECT metadata_json FROM nodes WHERE id=?",
979
- (file_node_id,),
980
- ).fetchone()
981
- source_id = None
982
- if file_row:
983
- source_id = _safe_loads(file_row["metadata_json"]).get("source_id")
984
-
985
- linked_rows = conn.execute(
986
- """
987
- SELECT n.id, n.type, n.metadata_json
988
- FROM edges e
989
- JOIN nodes n ON n.id=e.to_node
990
- WHERE e.from_node=?
991
- """,
992
- (file_node_id,),
993
- ).fetchall()
994
- owned_ids: set = set()
995
- auto_candidate_ids: set = set()
996
- for row in linked_rows:
997
- metadata = _safe_loads(row["metadata_json"])
998
- if (
999
- row["type"] in {"Chunk", "ImageText", "Section"}
1000
- or metadata.get("source_node") == file_node_id
1001
- ):
1002
- owned_ids.add(row["id"])
1003
- elif (
1004
- metadata.get("auto_extracted")
1005
- and metadata.get("source") == "local_folder"
1006
- ):
1007
- auto_candidate_ids.add(row["id"])
1008
-
1009
- conn.execute("DELETE FROM chunks WHERE source_node=?", (file_node_id,))
1010
- conn.execute(
1011
- "DELETE FROM edges WHERE from_node=? OR to_node=?",
1012
- (file_node_id, file_node_id),
1013
- )
1014
- conn.execute("DELETE FROM nodes WHERE id=?", (file_node_id,))
1015
- self._v2_delete_nodes(conn, [file_node_id])
1016
-
1017
- def delete_nodes(node_ids: set) -> None:
1018
- if not node_ids:
1019
- return
1020
- placeholders = ",".join("?" * len(node_ids))
1021
- params = list(node_ids)
1022
- conn.execute(
1023
- f"DELETE FROM chunks WHERE source_node IN ({placeholders})", params
1024
- )
1025
- conn.execute(
1026
- f"DELETE FROM edges WHERE from_node IN ({placeholders}) OR to_node IN ({placeholders})",
1027
- params * 2,
1028
- )
1029
- conn.execute(f"DELETE FROM nodes WHERE id IN ({placeholders})", params)
1030
- self._v2_delete_nodes(conn, params)
1031
-
1032
- delete_nodes(owned_ids)
1033
-
1034
- removable_auto_ids: set = set()
1035
- for node_id in auto_candidate_ids:
1036
- remaining_edges = conn.execute(
1037
- "SELECT from_node, to_node FROM edges WHERE from_node=? OR to_node=?",
1038
- (node_id, node_id),
1039
- ).fetchall()
1040
- if all(
1041
- (
1042
- row["from_node"] in auto_candidate_ids
1043
- and row["to_node"] in auto_candidate_ids
1044
- )
1045
- for row in remaining_edges
1046
- ):
1047
- removable_auto_ids.add(node_id)
1048
- delete_nodes(removable_auto_ids)
1049
- if source_id:
1050
- self._cleanup_local_graph_orphans(conn, str(source_id))
1051
-
1052
- def _cleanup_local_graph_orphans(
1053
- self, conn: sqlite3.Connection, source_id: str
1054
- ) -> None:
1055
- while True:
1056
- folder_rows = conn.execute(
1057
- "SELECT id, metadata_json FROM nodes WHERE type='Folder'"
1058
- ).fetchall()
1059
- leaf_ids = []
1060
- for row in folder_rows:
1061
- metadata = _safe_loads(row["metadata_json"])
1062
- if metadata.get("source_id") != source_id:
1063
- continue
1064
- has_children = conn.execute(
1065
- "SELECT 1 FROM edges WHERE from_node=? LIMIT 1",
1066
- (row["id"],),
1067
- ).fetchone()
1068
- if not has_children:
1069
- leaf_ids.append(row["id"])
1070
- if not leaf_ids:
1071
- break
1072
- placeholders = ",".join("?" * len(leaf_ids))
1073
- conn.execute(
1074
- f"DELETE FROM edges WHERE from_node IN ({placeholders}) OR to_node IN ({placeholders})",
1075
- leaf_ids * 2,
1076
- )
1077
- conn.execute(f"DELETE FROM nodes WHERE id IN ({placeholders})", leaf_ids)
1078
- self._v2_delete_nodes(conn, leaf_ids)
1079
-
1080
- for node_type in ("Drive", "Computer"):
1081
- rows = conn.execute(
1082
- "SELECT id FROM nodes WHERE type=?", (node_type,)
1083
- ).fetchall()
1084
- removable = []
1085
- for row in rows:
1086
- has_children = conn.execute(
1087
- "SELECT 1 FROM edges WHERE from_node=? LIMIT 1",
1088
- (row["id"],),
1089
- ).fetchone()
1090
- if not has_children:
1091
- removable.append(row["id"])
1092
- if removable:
1093
- placeholders = ",".join("?" * len(removable))
1094
- conn.execute(
1095
- f"DELETE FROM edges WHERE from_node IN ({placeholders}) OR to_node IN ({placeholders})",
1096
- removable * 2,
1097
- )
1098
- conn.execute(
1099
- f"DELETE FROM nodes WHERE id IN ({placeholders})", removable
1100
- )
1101
- self._v2_delete_nodes(conn, removable)
1102
-
1103
- def _local_file_index_has_extracted_text(self, row: sqlite3.Row) -> bool:
1104
- metadata = _safe_loads(row["metadata_json"])
1105
- parser = metadata.get("parser") if isinstance(metadata, dict) else {}
1106
- if not isinstance(parser, dict):
1107
- return False
1108
- try:
1109
- return int(parser.get("extracted_chars") or 0) > 0
1110
- except (TypeError, ValueError):
1111
- return False
1112
-
1113
- def index_local_folder(
1114
- self,
1115
- path: Path,
1116
- *,
1117
- include_ocr: bool = False,
1118
- watch_enabled: bool = False,
1119
- user_email: Optional[str] = None,
1120
- consent: Optional[Dict[str, Any]] = None,
1121
- max_files: int = 5_000,
1122
- ) -> Dict[str, Any]:
1123
- """Read approved files from a local folder and connect them to Graph RAG."""
1124
- root = Path(path).expanduser().resolve()
1125
- if not root.exists():
1126
- raise ValueError(f"경로가 존재하지 않습니다: {path}")
1127
- if not root.is_dir():
1128
- raise ValueError(f"폴더가 아닙니다: {path}")
1129
-
1130
- os_type = _current_os_type()
1131
- drive_id = _drive_id_for_path(root)
1132
- source_id = f"source:{_path_fingerprint(root)}"
1133
- now = _now()
1134
- max_files = max(1, min(int(max_files or 5_000), 50_000))
1135
- consent_payload = {
1136
- "approved_at": now,
1137
- "approved_by": user_email,
1138
- "knowledge_source": True,
1139
- "include_ocr": bool(include_ocr),
1140
- "watch_enabled": bool(watch_enabled),
1141
- "sensitive_files_default_excluded": True,
1142
- **(consent or {}),
1143
- }
1144
- counts: Counter = Counter()
1145
- seen_relative_paths: set = set()
1146
- indexed_nodes: List[str] = []
1147
- errors: List[Dict[str, str]] = []
1148
- limit_reached = False
1149
-
1150
- with self._connect() as conn:
1151
- conn.execute(
1152
- """
1153
- INSERT INTO knowledge_sources(
1154
- id, root_path, os_type, drive_id, label, status, include_ocr,
1155
- watch_enabled, consent_json, created_at, updated_at, last_scanned_at
1156
- )
1157
- VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)
1158
- ON CONFLICT(id) DO UPDATE SET
1159
- root_path=excluded.root_path,
1160
- os_type=excluded.os_type,
1161
- drive_id=excluded.drive_id,
1162
- label=excluded.label,
1163
- status=excluded.status,
1164
- include_ocr=excluded.include_ocr,
1165
- watch_enabled=excluded.watch_enabled,
1166
- consent_json=excluded.consent_json,
1167
- updated_at=excluded.updated_at,
1168
- last_scanned_at=excluded.last_scanned_at
1169
- """,
1170
- (
1171
- source_id,
1172
- str(root),
1173
- os_type,
1174
- drive_id,
1175
- root.name or str(root),
1176
- "scanning",
1177
- 1 if include_ocr else 0,
1178
- 1 if watch_enabled else 0,
1179
- _json(consent_payload),
1180
- now,
1181
- now,
1182
- now,
1183
- ),
1184
- )
1185
-
1186
- for entry in self._iter_local_scan_entries(root, max_files=max_files):
1187
- kind = entry["kind"]
1188
- file_path = entry["path"]
1189
- if kind == "limit_reached":
1190
- counts["limit_reached"] += 1
1191
- limit_reached = True
1192
- break
1193
- if kind in {"excluded_dir", "excluded"}:
1194
- counts["excluded"] += 1
1195
- continue
1196
- if kind in {"inaccessible_dir", "inaccessible_file"}:
1197
- counts["failed"] += 1
1198
- errors.append(
1199
- {
1200
- "path": str(file_path),
1201
- "error": entry.get("reason", "inaccessible"),
1202
- }
1203
- )
1204
- continue
1205
- if kind != "file":
1206
- continue
1207
-
1208
- stat = entry["stat"]
1209
- try:
1210
- relative_path = file_path.relative_to(root).as_posix()
1211
- except ValueError:
1212
- relative_path = file_path.name
1213
- seen_relative_paths.add(relative_path)
1214
- modified_at = _safe_iso_from_stat_mtime(stat.st_mtime)
1215
- existing = conn.execute(
1216
- """
1217
- SELECT size_bytes, modified_at, sha256, graph_node_id, status, metadata_json
1218
- FROM local_file_index
1219
- WHERE source_id=? AND relative_path=?
1220
- """,
1221
- (source_id, relative_path),
1222
- ).fetchone()
1223
- decision = self._local_file_decision(file_path, root, stat)
1224
- parser_type = decision["parser_type"]
1225
- if not decision["indexable"]:
1226
- counts[decision["status"]] += 1
1227
- if existing and existing["graph_node_id"]:
1228
- self._delete_local_file_graph(conn, existing["graph_node_id"])
1229
- self._upsert_local_file_index(
1230
- conn,
1231
- source_id=source_id,
1232
- root=root,
1233
- file_path=file_path,
1234
- stat=stat,
1235
- os_type=os_type,
1236
- drive_id=drive_id,
1237
- status=decision["status"],
1238
- parser_type=parser_type,
1239
- metadata={
1240
- "reason": decision["reason"],
1241
- "category": decision["category"],
1242
- },
1243
- )
1244
- continue
1245
-
1246
- if (
1247
- existing
1248
- and existing["status"] == "indexed"
1249
- and existing["graph_node_id"]
1250
- and self._local_file_index_has_extracted_text(existing)
1251
- and existing["size_bytes"] == stat.st_size
1252
- and existing["modified_at"] == modified_at
1253
- ):
1254
- counts["skipped_unchanged"] += 1
1255
- self._upsert_local_file_index(
1256
- conn,
1257
- source_id=source_id,
1258
- root=root,
1259
- file_path=file_path,
1260
- stat=stat,
1261
- os_type=os_type,
1262
- drive_id=drive_id,
1263
- status="indexed",
1264
- parser_type=parser_type,
1265
- sha256=existing["sha256"],
1266
- graph_node_id=existing["graph_node_id"],
1267
- metadata={
1268
- **_safe_loads(existing["metadata_json"]),
1269
- "category": decision["category"],
1270
- "unchanged": True,
1271
- },
1272
- )
1273
- continue
1274
-
1275
- try:
1276
- data = file_path.read_bytes()
1277
- digest = _sha256_bytes(data)
1278
- except Exception as exc:
1279
- counts["failed"] += 1
1280
- errors.append({"path": str(file_path), "error": str(exc)})
1281
- if existing and existing["graph_node_id"]:
1282
- self._delete_local_file_graph(conn, existing["graph_node_id"])
1283
- self._upsert_local_file_index(
1284
- conn,
1285
- source_id=source_id,
1286
- root=root,
1287
- file_path=file_path,
1288
- stat=stat,
1289
- os_type=os_type,
1290
- drive_id=drive_id,
1291
- status="failed",
1292
- parser_type=parser_type,
1293
- error_message=str(exc),
1294
- metadata={"category": decision["category"]},
1295
- )
1296
- continue
1297
-
1298
- if (
1299
- existing
1300
- and existing["sha256"] == digest
1301
- and existing["graph_node_id"]
1302
- and self._local_file_index_has_extracted_text(existing)
1303
- ):
1304
- counts["skipped_unchanged"] += 1
1305
- self._upsert_local_file_index(
1306
- conn,
1307
- source_id=source_id,
1308
- root=root,
1309
- file_path=file_path,
1310
- stat=stat,
1311
- os_type=os_type,
1312
- drive_id=drive_id,
1313
- status="indexed",
1314
- parser_type=parser_type,
1315
- sha256=digest,
1316
- graph_node_id=existing["graph_node_id"],
1317
- metadata={
1318
- **_safe_loads(existing["metadata_json"]),
1319
- "category": decision["category"],
1320
- "sha256_unchanged": True,
1321
- },
1322
- )
1323
- continue
1324
-
1325
- try:
1326
- text, parser_meta = self._extract_local_file_text(
1327
- file_path,
1328
- decision["category"],
1329
- include_ocr=include_ocr,
1330
- )
1331
- text = _clean_text(text)
1332
- parser_meta = {**parser_meta, "extracted_chars": len(text)}
1333
- if not text:
1334
- counts["skipped_empty_text"] += 1
1335
- if existing and existing["graph_node_id"]:
1336
- self._delete_local_file_graph(
1337
- conn, existing["graph_node_id"]
1338
- )
1339
- self._upsert_local_file_index(
1340
- conn,
1341
- source_id=source_id,
1342
- root=root,
1343
- file_path=file_path,
1344
- stat=stat,
1345
- os_type=os_type,
1346
- drive_id=drive_id,
1347
- status="skipped_empty_text",
1348
- parser_type=parser_type,
1349
- sha256=digest,
1350
- error_message="텍스트 추출 결과가 비어 있습니다.",
1351
- metadata={
1352
- "category": decision["category"],
1353
- "parser": parser_meta,
1354
- },
1355
- )
1356
- continue
1357
- graph_node_id = self._upsert_local_file_node(
1358
- conn,
1359
- source_id=source_id,
1360
- root=root,
1361
- file_path=file_path,
1362
- stat=stat,
1363
- os_type=os_type,
1364
- drive_id=drive_id,
1365
- sha256=digest,
1366
- category=decision["category"],
1367
- parser_type=parser_type,
1368
- text=text,
1369
- parser_meta=parser_meta,
1370
- )
1371
- self._upsert_local_file_index(
1372
- conn,
1373
- source_id=source_id,
1374
- root=root,
1375
- file_path=file_path,
1376
- stat=stat,
1377
- os_type=os_type,
1378
- drive_id=drive_id,
1379
- status="indexed",
1380
- parser_type=parser_type,
1381
- sha256=digest,
1382
- graph_node_id=graph_node_id,
1383
- metadata={
1384
- "category": decision["category"],
1385
- "parser": parser_meta,
1386
- },
1387
- )
1388
- counts["indexed"] += 1
1389
- indexed_nodes.append(graph_node_id)
1390
- except Exception as exc:
1391
- counts["failed"] += 1
1392
- errors.append({"path": str(file_path), "error": str(exc)})
1393
- if existing and existing["graph_node_id"]:
1394
- self._delete_local_file_graph(conn, existing["graph_node_id"])
1395
- self._upsert_local_file_index(
1396
- conn,
1397
- source_id=source_id,
1398
- root=root,
1399
- file_path=file_path,
1400
- stat=stat,
1401
- os_type=os_type,
1402
- drive_id=drive_id,
1403
- status="failed",
1404
- parser_type=parser_type,
1405
- sha256=digest,
1406
- error_message=str(exc),
1407
- metadata={"category": decision["category"]},
1408
- )
1409
-
1410
- if not limit_reached:
1411
- existing_rows = {
1412
- row["relative_path"]: row["graph_node_id"]
1413
- for row in conn.execute(
1414
- "SELECT relative_path, graph_node_id FROM local_file_index WHERE source_id=?",
1415
- (source_id,),
1416
- )
1417
- }
1418
- deleted_paths = set(existing_rows) - seen_relative_paths
1419
- for relative_path in deleted_paths:
1420
- self._delete_local_file_graph(
1421
- conn, existing_rows.get(relative_path)
1422
- )
1423
- conn.execute(
1424
- """
1425
- UPDATE local_file_index
1426
- SET status='deleted', deleted=1, last_scanned_at=?, error_message=NULL, graph_node_id=NULL
1427
- WHERE source_id=? AND relative_path=?
1428
- """,
1429
- (_now(), source_id, relative_path),
1430
- )
1431
- counts["deleted"] = len(deleted_paths)
1432
- conn.execute(
1433
- """
1434
- UPDATE knowledge_sources
1435
- SET status='active', updated_at=?, last_scanned_at=?
1436
- WHERE id=?
1437
- """,
1438
- (_now(), _now(), source_id),
1439
- )
1440
-
1441
- return {
1442
- "status": "ok",
1443
- "source": {
1444
- "id": source_id,
1445
- "root_path": str(root),
1446
- "os_type": os_type,
1447
- "drive_id": drive_id,
1448
- "include_ocr": bool(include_ocr),
1449
- "watch_enabled": bool(watch_enabled),
1450
- },
1451
- "counts": dict(counts),
1452
- "indexed_nodes": indexed_nodes[:100],
1453
- "errors": errors[:50],
1454
- "notice": "Lattice AI는 사용자가 선택한 폴더만 AI 지식으로 변환합니다.",
1455
- }