ltcai 8.9.0 → 9.1.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 (208) hide show
  1. package/README.md +81 -58
  2. package/auto_setup.py +7 -904
  3. package/desktop/electron/README.md +9 -0
  4. package/desktop/electron/main.cjs +5 -3
  5. package/docs/CHANGELOG.md +221 -238
  6. package/docs/COMMUNITY_AND_PLUGINS.md +3 -2
  7. package/docs/DEVELOPMENT.md +53 -14
  8. package/docs/LEGACY_COMPATIBILITY.md +4 -4
  9. package/docs/ONBOARDING.md +10 -2
  10. package/docs/OPERATIONS.md +8 -4
  11. package/docs/PRODUCT_DIRECTION_REVIEW.md +4 -0
  12. package/docs/ROADMAP_RECOMMENDATIONS.md +61 -36
  13. package/docs/TRUST_MODEL.md +5 -2
  14. package/docs/WHY_LATTICE.md +15 -10
  15. package/docs/WORKFLOW_DESIGNER.md +22 -0
  16. package/docs/kg-schema.md +13 -2
  17. package/docs/mcp-tools.md +17 -6
  18. package/docs/privacy.md +19 -3
  19. package/docs/public-deploy.md +32 -3
  20. package/docs/security-model.md +46 -11
  21. package/lattice_brain/__init__.py +1 -1
  22. package/lattice_brain/archive.py +4 -14
  23. package/lattice_brain/context.py +66 -9
  24. package/lattice_brain/embeddings.py +38 -2
  25. package/lattice_brain/graph/_kg_common.py +27 -462
  26. package/lattice_brain/graph/_kg_constants.py +243 -0
  27. package/lattice_brain/graph/_kg_fsutil.py +293 -0
  28. package/lattice_brain/graph/discovery.py +0 -948
  29. package/lattice_brain/graph/discovery_index.py +1126 -0
  30. package/lattice_brain/graph/documents.py +44 -9
  31. package/lattice_brain/graph/ingest.py +47 -20
  32. package/lattice_brain/graph/provenance.py +13 -6
  33. package/lattice_brain/graph/retrieval.py +141 -610
  34. package/lattice_brain/graph/retrieval_docgen.py +243 -0
  35. package/lattice_brain/graph/retrieval_vector.py +460 -0
  36. package/lattice_brain/graph/store.py +6 -0
  37. package/lattice_brain/ingestion.py +69 -4
  38. package/lattice_brain/portability.py +29 -23
  39. package/lattice_brain/quality.py +98 -4
  40. package/lattice_brain/runtime/agent_runtime.py +169 -7
  41. package/lattice_brain/runtime/hooks.py +30 -13
  42. package/lattice_brain/runtime/multi_agent.py +27 -8
  43. package/lattice_brain/runtime/statuses.py +10 -0
  44. package/lattice_brain/utils.py +46 -0
  45. package/lattice_brain/workflow.py +1 -5
  46. package/latticeai/__init__.py +1 -1
  47. package/latticeai/api/admin.py +1 -1
  48. package/latticeai/api/agent_registry.py +10 -8
  49. package/latticeai/api/agents.py +22 -3
  50. package/latticeai/api/auth.py +78 -16
  51. package/latticeai/api/browser.py +303 -34
  52. package/latticeai/api/chat.py +355 -952
  53. package/latticeai/api/chat_agent_http.py +356 -0
  54. package/latticeai/api/chat_contracts.py +54 -0
  55. package/latticeai/api/chat_documents.py +256 -0
  56. package/latticeai/api/chat_helpers.py +250 -0
  57. package/latticeai/api/chat_history.py +99 -0
  58. package/latticeai/api/chat_intents.py +302 -0
  59. package/latticeai/api/chat_stream.py +115 -0
  60. package/latticeai/api/computer_use.py +211 -40
  61. package/latticeai/api/health.py +9 -3
  62. package/latticeai/api/hooks.py +17 -6
  63. package/latticeai/api/knowledge_graph.py +78 -14
  64. package/latticeai/api/local_files.py +7 -2
  65. package/latticeai/api/marketplace.py +11 -0
  66. package/latticeai/api/mcp.py +102 -23
  67. package/latticeai/api/models.py +72 -33
  68. package/latticeai/api/network.py +5 -5
  69. package/latticeai/api/permissions.py +70 -29
  70. package/latticeai/api/plugins.py +21 -7
  71. package/latticeai/api/portability.py +5 -5
  72. package/latticeai/api/realtime.py +33 -5
  73. package/latticeai/api/setup.py +2 -2
  74. package/latticeai/api/static_routes.py +123 -24
  75. package/latticeai/api/tools.py +97 -14
  76. package/latticeai/api/workflow_designer.py +37 -0
  77. package/latticeai/api/workspace.py +2 -1
  78. package/latticeai/app_factory.py +185 -405
  79. package/latticeai/core/agent.py +19 -9
  80. package/latticeai/core/agent_registry.py +1 -5
  81. package/latticeai/core/config.py +23 -3
  82. package/latticeai/core/context_builder.py +21 -3
  83. package/latticeai/core/invitations.py +1 -4
  84. package/latticeai/core/io_utils.py +45 -0
  85. package/latticeai/core/legacy_compatibility.py +24 -3
  86. package/latticeai/core/local_embeddings.py +2 -4
  87. package/latticeai/core/marketplace.py +33 -2
  88. package/latticeai/core/mcp_catalog.py +450 -0
  89. package/latticeai/core/mcp_registry.py +2 -441
  90. package/latticeai/core/plugins.py +15 -0
  91. package/latticeai/core/policy.py +1 -1
  92. package/latticeai/core/realtime.py +38 -15
  93. package/latticeai/core/sessions.py +7 -2
  94. package/latticeai/core/timeutil.py +10 -0
  95. package/latticeai/core/tool_registry.py +90 -18
  96. package/latticeai/core/users.py +5 -14
  97. package/latticeai/core/workspace_graph_trace.py +31 -5
  98. package/latticeai/core/workspace_memory.py +3 -1
  99. package/latticeai/core/workspace_os.py +18 -7
  100. package/latticeai/core/workspace_os_utils.py +2 -21
  101. package/latticeai/core/workspace_permissions.py +2 -1
  102. package/latticeai/core/workspace_plugins.py +1 -1
  103. package/latticeai/core/workspace_runs.py +14 -5
  104. package/latticeai/core/workspace_skills.py +1 -1
  105. package/latticeai/core/workspace_snapshots.py +2 -1
  106. package/latticeai/core/workspace_timeline.py +2 -1
  107. package/latticeai/integrations/telegram_bot.py +96 -40
  108. package/latticeai/models/model_providers.py +111 -0
  109. package/latticeai/models/router.py +189 -173
  110. package/latticeai/runtime/access_runtime.py +62 -4
  111. package/latticeai/runtime/audit_runtime.py +27 -16
  112. package/latticeai/runtime/automation_runtime.py +22 -7
  113. package/latticeai/runtime/brain_runtime.py +19 -7
  114. package/latticeai/runtime/chat_wiring.py +2 -0
  115. package/latticeai/runtime/config_runtime.py +58 -26
  116. package/latticeai/runtime/context_runtime.py +16 -4
  117. package/latticeai/runtime/history_runtime.py +163 -0
  118. package/latticeai/runtime/hooks_runtime.py +1 -1
  119. package/latticeai/runtime/model_wiring.py +33 -5
  120. package/latticeai/runtime/namespace_runtime.py +163 -0
  121. package/latticeai/runtime/network_config_runtime.py +56 -0
  122. package/latticeai/runtime/platform_runtime_wiring.py +4 -3
  123. package/latticeai/runtime/review_wiring.py +1 -1
  124. package/latticeai/runtime/router_registration.py +34 -1
  125. package/latticeai/runtime/security_runtime.py +110 -13
  126. package/latticeai/runtime/sso_config_runtime.py +128 -0
  127. package/latticeai/runtime/stages.py +27 -0
  128. package/latticeai/runtime/user_key_runtime.py +106 -0
  129. package/latticeai/server_app.py +5 -1
  130. package/latticeai/services/architecture_readiness.py +74 -5
  131. package/latticeai/services/brain_automation.py +3 -26
  132. package/latticeai/services/chat_service.py +205 -15
  133. package/latticeai/services/local_knowledge.py +423 -0
  134. package/latticeai/services/memory_service.py +268 -21
  135. package/latticeai/services/model_engines.py +48 -33
  136. package/latticeai/services/model_errors.py +17 -0
  137. package/latticeai/services/model_loading.py +28 -25
  138. package/latticeai/services/model_runtime.py +228 -162
  139. package/latticeai/services/p_reinforce.py +22 -3
  140. package/latticeai/services/platform_runtime.py +92 -24
  141. package/latticeai/services/product_readiness.py +19 -17
  142. package/latticeai/services/review_queue.py +76 -11
  143. package/latticeai/services/router_context.py +1 -0
  144. package/latticeai/services/run_executor.py +25 -9
  145. package/latticeai/services/search_service.py +203 -28
  146. package/latticeai/services/setup_detection.py +80 -0
  147. package/latticeai/services/tool_dispatch.py +28 -5
  148. package/latticeai/services/triggers.py +53 -4
  149. package/latticeai/services/upload_service.py +13 -2
  150. package/latticeai/setup/__init__.py +25 -0
  151. package/latticeai/setup/auto_setup.py +857 -0
  152. package/latticeai/setup/wizard.py +1264 -0
  153. package/latticeai/tools/__init__.py +278 -0
  154. package/{tools → latticeai/tools}/commands.py +67 -7
  155. package/{tools → latticeai/tools}/computer.py +1 -1
  156. package/{tools → latticeai/tools}/documents.py +1 -1
  157. package/{tools → latticeai/tools}/filesystem.py +3 -3
  158. package/latticeai/tools/knowledge.py +178 -0
  159. package/{tools → latticeai/tools}/local_files.py +7 -1
  160. package/{tools → latticeai/tools}/network.py +0 -1
  161. package/local_knowledge_api.py +4 -342
  162. package/package.json +22 -2
  163. package/scripts/brain_quality_eval.py +3 -1
  164. package/scripts/bump_version.py +3 -0
  165. package/scripts/capture_release_evidence.mjs +180 -0
  166. package/scripts/check_current_release_docs.mjs +142 -0
  167. package/scripts/check_i18n_literals.mjs +107 -31
  168. package/scripts/check_openapi_drift.mjs +56 -0
  169. package/scripts/export_openapi.py +67 -7
  170. package/scripts/i18n_literal_allowlist.json +22 -24
  171. package/scripts/run_integration_tests.mjs +72 -10
  172. package/scripts/validate_release_artifacts.py +49 -7
  173. package/scripts/wheel_smoke.py +5 -0
  174. package/setup_wizard.py +3 -1304
  175. package/src-tauri/Cargo.lock +1 -1
  176. package/src-tauri/Cargo.toml +1 -1
  177. package/src-tauri/tauri.conf.json +1 -1
  178. package/static/app/asset-manifest.json +11 -11
  179. package/static/app/assets/Act-Bzz0bUyW.js +1 -0
  180. package/static/app/assets/Brain-Dj2J20YA.js +321 -0
  181. package/static/app/assets/Capture-CqlEl1Ga.js +1 -0
  182. package/static/app/assets/Library-B03FP1Yx.js +1 -0
  183. package/static/app/assets/System-80lHW0Ux.js +1 -0
  184. package/static/app/assets/index-BiMofBTM.js +17 -0
  185. package/static/app/assets/index-Bmx9rzTc.css +2 -0
  186. package/static/app/assets/primitives-Q1A96_7v.js +1 -0
  187. package/static/app/assets/textarea-D13RtnTo.js +1 -0
  188. package/static/app/index.html +2 -2
  189. package/static/css/tokens.css +4 -2
  190. package/static/sw.js +1 -1
  191. package/tools/__init__.py +21 -269
  192. package/docs/CODE_REVIEW_2026-07-06.md +0 -764
  193. package/latticeai/runtime/app_context_runtime.py +0 -13
  194. package/latticeai/runtime/sso_runtime.py +0 -52
  195. package/latticeai/runtime/tail_wiring.py +0 -21
  196. package/scripts/com.pts.claudecode.discord.plist +0 -31
  197. package/scripts/pts-claudecode-discord-bridge.mjs +0 -207
  198. package/scripts/start-pts-claudecode-discord.sh +0 -51
  199. package/static/app/assets/Act-fZokUnC0.js +0 -1
  200. package/static/app/assets/Brain-DtyuWubr.js +0 -321
  201. package/static/app/assets/Capture-D5KV3Cu7.js +0 -1
  202. package/static/app/assets/Library-C9kyFkSt.js +0 -1
  203. package/static/app/assets/System-VbChmX7r.js +0 -1
  204. package/static/app/assets/index-DCh5AoXt.css +0 -2
  205. package/static/app/assets/index-DPdcPoF0.js +0 -17
  206. package/static/app/assets/primitives-DFeanEV6.js +0 -1
  207. package/static/app/assets/textarea-CD8UNKIy.js +0 -1
  208. package/tools/knowledge.py +0 -95
@@ -0,0 +1,423 @@
1
+ """Local folder knowledge-source API and optional filesystem watcher."""
2
+
3
+ import logging
4
+ import threading
5
+ from pathlib import Path
6
+ from typing import Any, Callable, Dict, Optional
7
+
8
+ from fastapi import APIRouter, HTTPException, Request
9
+ from pydantic import BaseModel, Field
10
+
11
+
12
+ class LocalTreeRequest(BaseModel):
13
+ path: str
14
+ max_items: int = 200
15
+ approved: bool = False
16
+ approval_token: Optional[str] = None
17
+
18
+
19
+ class LocalKnowledgeAuditRequest(BaseModel):
20
+ path: str
21
+ include_ocr: bool = False
22
+ max_files: int = 50_000
23
+ approved: bool = False
24
+ approval_token: Optional[str] = None
25
+
26
+
27
+ class LocalKnowledgeIndexRequest(BaseModel):
28
+ path: str
29
+ include_ocr: bool = False
30
+ watch_enabled: bool = False
31
+ max_files: int = 5_000
32
+ consent: Dict[str, Any] = Field(default_factory=dict)
33
+ approved: bool = False
34
+ approval_token: Optional[str] = None
35
+
36
+
37
+ class LocalKnowledgeWatchRequest(BaseModel):
38
+ source_id: str
39
+
40
+
41
+ class _LocalWatchHandler:
42
+ def __init__(self, schedule_change: Callable[[], None]):
43
+ self._schedule_change = schedule_change
44
+
45
+ def on_any_event(self, event): # pragma: no cover - exercised by OS watcher
46
+ if getattr(event, "is_directory", False):
47
+ return
48
+ self._schedule_change()
49
+
50
+
51
+ class LocalKnowledgeWatcher:
52
+ """Debounced watchdog wrapper for approved local knowledge sources."""
53
+
54
+ def __init__(self, get_graph: Callable[[], Any], *, debounce_seconds: float = 5.0, hooks: Any = None):
55
+ self._get_graph = get_graph
56
+ self._debounce_seconds = debounce_seconds
57
+ self._hooks = hooks
58
+ self._lock = threading.Lock()
59
+ self._watched: Dict[str, Dict[str, Any]] = {}
60
+ self._observer_cls = None
61
+ self._event_handler_base = None
62
+ self._import_error = ""
63
+ try:
64
+ from watchdog.events import FileSystemEventHandler
65
+ from watchdog.observers import Observer
66
+
67
+ self._observer_cls = Observer
68
+ self._event_handler_base = FileSystemEventHandler
69
+ except Exception as exc: # pragma: no cover - depends on optional dependency
70
+ self._import_error = str(exc)
71
+
72
+ @property
73
+ def available(self) -> bool:
74
+ return self._observer_cls is not None and self._event_handler_base is not None
75
+
76
+ def status(self) -> Dict[str, Any]:
77
+ with self._lock:
78
+ active = {
79
+ source_id: {
80
+ "root_path": item["source"].get("root_path"),
81
+ "last_event_at": item.get("last_event_at"),
82
+ "last_indexed_at": item.get("last_indexed_at"),
83
+ "last_error": item.get("last_error"),
84
+ }
85
+ for source_id, item in self._watched.items()
86
+ }
87
+ return {
88
+ "available": self.available,
89
+ "error": "" if self.available else self._import_error or "watchdog is not installed",
90
+ "debounce_seconds": self._debounce_seconds,
91
+ "active": active,
92
+ }
93
+
94
+ def restore_enabled_sources(self) -> Dict[str, Any]:
95
+ graph = self._get_graph()
96
+ if graph is None:
97
+ return {"restored": 0, "available": self.available}
98
+ restored = 0
99
+ try:
100
+ for source in graph.local_sources().get("sources", []):
101
+ if source.get("watch_enabled"):
102
+ consent = source.get("consent") or {}
103
+ restored_source = {
104
+ **source,
105
+ # Older sources had no explicit scope. Their local
106
+ # knowledge belongs to the personal Brain, never every
107
+ # workspace.
108
+ "workspace_id": source.get("workspace_id") or consent.get("workspace_id") or "personal",
109
+ }
110
+ result = self.start_source(restored_source)
111
+ if result.get("watching"):
112
+ restored += 1
113
+ except Exception as exc:
114
+ logging.warning("local knowledge watcher restore failed: %s", exc)
115
+ return {"restored": restored, "available": self.available}
116
+
117
+ def start_source(self, source: Dict[str, Any]) -> Dict[str, Any]:
118
+ source_id = str(source.get("id") or "")
119
+ root_path = str(source.get("root_path") or "")
120
+ if not source_id or not root_path:
121
+ return {"watching": False, "error": "source_id and root_path are required"}
122
+ if not self.available:
123
+ return {"watching": False, "source_id": source_id, "error": self._import_error or "watchdog is not installed"}
124
+ root = Path(root_path).expanduser().resolve()
125
+ if not root.exists() or not root.is_dir():
126
+ return {"watching": False, "source_id": source_id, "error": "source folder is not available"}
127
+
128
+ self.stop_source(source_id)
129
+
130
+ class Handler(_LocalWatchHandler, self._event_handler_base): # type: ignore[misc, valid-type]
131
+ def __init__(handler_self):
132
+ self._event_handler_base.__init__(handler_self)
133
+ _LocalWatchHandler.__init__(handler_self, lambda: self._schedule(source_id))
134
+
135
+ observer = self._observer_cls()
136
+ try:
137
+ observer.schedule(Handler(), str(root), recursive=True)
138
+ observer.start()
139
+ except Exception as exc:
140
+ logging.warning("local knowledge watcher start failed for %s: %s", root, exc)
141
+ return {"watching": False, "source_id": source_id, "error": str(exc)}
142
+
143
+ with self._lock:
144
+ self._watched[source_id] = {
145
+ "observer": observer,
146
+ "timer": None,
147
+ "source": dict(source),
148
+ "last_event_at": None,
149
+ "last_indexed_at": None,
150
+ "last_error": None,
151
+ }
152
+ return {"watching": True, "source_id": source_id, "root_path": str(root)}
153
+
154
+ def stop_source(self, source_id: str) -> Dict[str, Any]:
155
+ with self._lock:
156
+ item = self._watched.pop(source_id, None)
157
+ if not item:
158
+ return {"stopped": False, "source_id": source_id}
159
+ timer = item.get("timer")
160
+ if timer:
161
+ timer.cancel()
162
+ observer = item.get("observer")
163
+ try:
164
+ observer.stop()
165
+ observer.join(timeout=3)
166
+ except Exception as exc:
167
+ logging.warning("local knowledge watcher stop failed for %s: %s", source_id, exc)
168
+ return {"stopped": True, "source_id": source_id}
169
+
170
+ def stop_all(self) -> None:
171
+ for source_id in list(self.status().get("active", {}).keys()):
172
+ self.stop_source(source_id)
173
+
174
+ def _schedule(self, source_id: str) -> None:
175
+ with self._lock:
176
+ item = self._watched.get(source_id)
177
+ if not item:
178
+ return
179
+ timer = item.get("timer")
180
+ if timer:
181
+ timer.cancel()
182
+ item["last_event_at"] = _now_seconds()
183
+ timer = threading.Timer(self._debounce_seconds, self._run_index, args=(source_id,))
184
+ timer.daemon = True
185
+ item["timer"] = timer
186
+ timer.start()
187
+
188
+ def _run_index(self, source_id: str) -> None:
189
+ with self._lock:
190
+ item = self._watched.get(source_id)
191
+ if not item:
192
+ return
193
+ source = dict(item["source"])
194
+ item["timer"] = None
195
+ graph = self._get_graph()
196
+ if graph is None:
197
+ return
198
+ consent = source.get("consent") or {}
199
+ root = source.get("root_path")
200
+ if self._hooks is not None:
201
+ self._hooks.fire_hook("pre_index", "folder.reindex",
202
+ payload={"source_id": source_id, "root_path": root, "trigger": "watch"})
203
+ try:
204
+ result = graph.index_local_folder(
205
+ Path(source["root_path"]),
206
+ include_ocr=bool(source.get("include_ocr")),
207
+ watch_enabled=True,
208
+ user_email=consent.get("approved_by"),
209
+ workspace_id=source.get("workspace_id") or consent.get("workspace_id") or "personal",
210
+ consent=consent,
211
+ source_id_override=source_id,
212
+ )
213
+ with self._lock:
214
+ if source_id in self._watched:
215
+ self._watched[source_id]["last_indexed_at"] = _now_seconds()
216
+ self._watched[source_id]["last_error"] = None
217
+ if self._hooks is not None:
218
+ self._hooks.fire_hook("post_index", "folder.reindex",
219
+ payload={"source_id": source_id, "root_path": root, "trigger": "watch", "status": "ok"})
220
+ counts = (result.get("counts") or {}) if isinstance(result, dict) else {}
221
+ if int(counts.get("indexed") or 0) + int(counts.get("deleted") or 0) > 0:
222
+ self._hooks.fire_hook(
223
+ "post_tool",
224
+ "tool.kg_ingest.local_folder",
225
+ payload={
226
+ "tool": "kg_ingest.local_folder",
227
+ "status": "ok",
228
+ "source": "ingestion",
229
+ "source_type": "local_folder",
230
+ "source_id": source_id,
231
+ },
232
+ user_email=consent.get("approved_by"),
233
+ workspace_id=source.get("workspace_id"),
234
+ )
235
+ except Exception as exc:
236
+ logging.warning("local knowledge watcher reindex failed for %s: %s", source_id, exc)
237
+ with self._lock:
238
+ if source_id in self._watched:
239
+ self._watched[source_id]["last_error"] = str(exc)
240
+ if self._hooks is not None:
241
+ self._hooks.fire_hook("post_index", "folder.reindex",
242
+ payload={"source_id": source_id, "root_path": root, "trigger": "watch", "status": "error", "error": str(exc)})
243
+
244
+
245
+ def _now_seconds() -> float:
246
+ import time
247
+
248
+ return time.time()
249
+
250
+
251
+ def create_local_knowledge_router(
252
+ *,
253
+ get_graph: Callable[[], Any],
254
+ require_graph: Callable[[], None],
255
+ require_user: Callable[[Request], str],
256
+ require_local_user: Callable[[Request], str],
257
+ local_permission_response: Callable[..., dict],
258
+ require_local_approval: Callable[..., None],
259
+ watcher: Optional[LocalKnowledgeWatcher] = None,
260
+ hooks: Any = None,
261
+ workspace_service: Any = None,
262
+ ) -> APIRouter:
263
+ router = APIRouter()
264
+
265
+ def graph():
266
+ require_graph()
267
+ return get_graph()
268
+
269
+ def write_workspace(request: Request, user: str) -> Optional[str]:
270
+ requested = request.headers.get("X-Workspace-Id")
271
+ requested = requested.strip() if requested and requested.strip() else None
272
+ if workspace_service is None:
273
+ return requested
274
+ try:
275
+ return workspace_service.resolve_write_scope(requested, user or None)
276
+ except PermissionError as exc:
277
+ raise HTTPException(status_code=403, detail=str(exc)) from exc
278
+
279
+ @router.get("/knowledge-graph/local/roots")
280
+ async def knowledge_graph_local_roots(request: Request):
281
+ require_user(request)
282
+ return graph().discover_local_roots()
283
+
284
+ @router.get("/knowledge-graph/local/sources")
285
+ async def knowledge_graph_local_sources(request: Request):
286
+ require_user(request)
287
+ payload = graph().local_sources()
288
+ watch_status = watcher.status() if watcher else {"available": False, "active": {}}
289
+ active = watch_status.get("active", {})
290
+ for source in payload.get("sources", []):
291
+ source["watch_active"] = source.get("id") in active
292
+ source["watch_status"] = active.get(source.get("id"))
293
+ payload["watch"] = watch_status
294
+ return payload
295
+
296
+ @router.get("/knowledge-graph/local/watch/status")
297
+ async def knowledge_graph_local_watch_status(request: Request):
298
+ require_user(request)
299
+ graph()
300
+ return watcher.status() if watcher else {"available": False, "active": {}, "error": "watcher unavailable"}
301
+
302
+ @router.post("/knowledge-graph/local/watch/stop")
303
+ async def knowledge_graph_local_watch_stop(req: LocalKnowledgeWatchRequest, request: Request):
304
+ require_user(request)
305
+ kg = graph()
306
+ try:
307
+ kg.set_local_source_watch(req.source_id, False)
308
+ except ValueError as exc:
309
+ raise HTTPException(status_code=404, detail=str(exc)) from exc
310
+ result = watcher.stop_source(req.source_id) if watcher else {"stopped": False, "source_id": req.source_id}
311
+ return {"status": "ok", "watch": result}
312
+
313
+ @router.post("/knowledge-graph/local/tree")
314
+ async def knowledge_graph_local_tree(req: LocalTreeRequest, request: Request):
315
+ current_user = require_local_user(request)
316
+ kg = graph()
317
+ if not req.approved:
318
+ return local_permission_response(req.path, "list", current_user)
319
+ require_local_approval(token=req.approval_token, path=req.path, action="list", user_email=current_user)
320
+ try:
321
+ return kg.preview_local_tree(Path(req.path), max_items=req.max_items)
322
+ except ValueError as exc:
323
+ raise HTTPException(status_code=400, detail=str(exc)) from exc
324
+
325
+ @router.post("/knowledge-graph/local/audit")
326
+ async def knowledge_graph_local_audit(req: LocalKnowledgeAuditRequest, request: Request):
327
+ current_user = require_local_user(request)
328
+ kg = graph()
329
+ if not req.approved:
330
+ return local_permission_response(req.path, "list", current_user)
331
+ require_local_approval(token=req.approval_token, path=req.path, action="list", user_email=current_user)
332
+ try:
333
+ return kg.audit_local_folder(Path(req.path), include_ocr=req.include_ocr, max_files=req.max_files)
334
+ except ValueError as exc:
335
+ raise HTTPException(status_code=400, detail=str(exc)) from exc
336
+
337
+ @router.post("/knowledge-graph/local/index")
338
+ async def knowledge_graph_local_index(req: LocalKnowledgeIndexRequest, request: Request):
339
+ current_user = require_local_user(request)
340
+ workspace_id = write_workspace(request, current_user)
341
+ kg = graph()
342
+ if not req.approved:
343
+ return local_permission_response(req.path, "read", current_user)
344
+ require_local_approval(token=req.approval_token, path=req.path, action="read", user_email=current_user)
345
+ consent = {
346
+ **(req.consent or {}),
347
+ "approved_by": current_user,
348
+ "workspace_id": workspace_id or "personal",
349
+ }
350
+ source_id_override = None
351
+ try:
352
+ target_root = Path(req.path).expanduser().resolve()
353
+ target_scope = workspace_id or "personal"
354
+ for source in kg.local_sources().get("sources", []):
355
+ source_consent = source.get("consent") or {}
356
+ source_scope = source_consent.get("workspace_id") or "personal"
357
+ source_root_value = str(source.get("root_path") or "").strip()
358
+ if not source_root_value:
359
+ continue
360
+ source_root = Path(source_root_value).expanduser().resolve()
361
+ if source_scope == target_scope and source_root == target_root:
362
+ source_id_override = source.get("id")
363
+ break
364
+ except Exception:
365
+ # Source reuse is a compatibility optimization; indexing still has
366
+ # a deterministic workspace-scoped ID when discovery is unavailable.
367
+ source_id_override = None
368
+ if hooks is not None:
369
+ hooks.fire_hook("pre_index", "folder.index",
370
+ payload={"root_path": req.path, "trigger": "connect", "watch": req.watch_enabled},
371
+ user_email=current_user, workspace_id=workspace_id)
372
+ try:
373
+ result = kg.index_local_folder(
374
+ Path(req.path),
375
+ include_ocr=req.include_ocr,
376
+ watch_enabled=req.watch_enabled,
377
+ user_email=current_user,
378
+ workspace_id=workspace_id or "personal",
379
+ consent=consent,
380
+ max_files=req.max_files,
381
+ source_id_override=source_id_override,
382
+ )
383
+ except ValueError as exc:
384
+ if hooks is not None:
385
+ hooks.fire_hook("post_index", "folder.index",
386
+ payload={"root_path": req.path, "trigger": "connect", "status": "error", "error": str(exc)},
387
+ user_email=current_user, workspace_id=workspace_id)
388
+ raise HTTPException(status_code=400, detail=str(exc)) from exc
389
+ if hooks is not None:
390
+ _idx = (result.get("index") or {}) if isinstance(result, dict) else {}
391
+ hooks.fire_hook("post_index", "folder.index",
392
+ payload={"root_path": req.path, "trigger": "connect", "status": "ok",
393
+ "indexed": _idx.get("indexed") or (result or {}).get("indexed")},
394
+ user_email=current_user, workspace_id=workspace_id)
395
+ counts = (result.get("counts") or {}) if isinstance(result, dict) else {}
396
+ if int(counts.get("indexed") or 0) + int(counts.get("deleted") or 0) > 0:
397
+ hooks.fire_hook(
398
+ "post_tool",
399
+ "tool.kg_ingest.local_folder",
400
+ payload={
401
+ "tool": "kg_ingest.local_folder",
402
+ "status": "ok",
403
+ "source": "ingestion",
404
+ "source_type": "local_folder",
405
+ "source_id": (result.get("source") or {}).get("id") if isinstance(result, dict) else None,
406
+ },
407
+ user_email=current_user,
408
+ workspace_id=workspace_id,
409
+ )
410
+
411
+ if watcher:
412
+ if req.watch_enabled:
413
+ source_payload = {
414
+ **result.get("source", {}),
415
+ "workspace_id": workspace_id or "personal",
416
+ "consent": consent,
417
+ }
418
+ result["watch"] = watcher.start_source(source_payload)
419
+ else:
420
+ result["watch"] = watcher.stop_source(result.get("source", {}).get("id", ""))
421
+ return result
422
+
423
+ return router