ltcai 4.0.0 → 4.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 (195) hide show
  1. package/README.md +42 -33
  2. package/desktop/electron/main.cjs +44 -0
  3. package/docs/CHANGELOG.md +106 -0
  4. package/docs/REALTIME_COLLABORATION.md +3 -3
  5. package/docs/V3_FRONTEND.md +9 -8
  6. package/docs/V4_1_FRONTEND_ARCHITECTURE_REVIEW.md +65 -0
  7. package/docs/V4_1_FRONTEND_MIGRATION_REPORT.md +70 -0
  8. package/docs/V4_1_VALIDATION_REPORT.md +47 -0
  9. package/docs/V4_DIGITAL_BRAIN_RECOVERY.md +95 -45
  10. package/docs/kg-schema.md +6 -2
  11. package/docs/spec-vs-impl.md +10 -10
  12. package/frontend/index.html +24 -0
  13. package/frontend/openapi.json +14190 -0
  14. package/frontend/src/App.tsx +184 -0
  15. package/frontend/src/api/client.ts +317 -0
  16. package/frontend/src/api/openapi.ts +16637 -0
  17. package/frontend/src/components/primitives.tsx +204 -0
  18. package/frontend/src/components/ui/badge.tsx +27 -0
  19. package/frontend/src/components/ui/button.tsx +37 -0
  20. package/frontend/src/components/ui/card.tsx +22 -0
  21. package/frontend/src/components/ui/input.tsx +16 -0
  22. package/frontend/src/components/ui/textarea.tsx +16 -0
  23. package/frontend/src/lib/utils.ts +33 -0
  24. package/frontend/src/main.tsx +23 -0
  25. package/frontend/src/pages/Act.tsx +245 -0
  26. package/frontend/src/pages/Ask.tsx +200 -0
  27. package/frontend/src/pages/Brain.tsx +267 -0
  28. package/frontend/src/pages/Capture.tsx +158 -0
  29. package/frontend/src/pages/Library.tsx +187 -0
  30. package/frontend/src/pages/System.tsx +344 -0
  31. package/frontend/src/routes.ts +85 -0
  32. package/frontend/src/store/appStore.ts +54 -0
  33. package/frontend/src/styles.css +107 -0
  34. package/kg_schema.py +2 -603
  35. package/knowledge_graph.py +37 -4958
  36. package/latticeai/__init__.py +1 -1
  37. package/latticeai/api/admin.py +15 -16
  38. package/latticeai/api/agents.py +13 -6
  39. package/latticeai/api/auth.py +19 -11
  40. package/latticeai/api/invitations.py +100 -0
  41. package/latticeai/api/knowledge_graph.py +4 -11
  42. package/latticeai/api/plugins.py +3 -6
  43. package/latticeai/api/realtime.py +4 -7
  44. package/latticeai/api/setup.py +5 -4
  45. package/latticeai/api/static_routes.py +13 -16
  46. package/latticeai/api/ui_redirects.py +26 -0
  47. package/latticeai/api/workflow_designer.py +39 -6
  48. package/latticeai/api/workspace.py +24 -10
  49. package/latticeai/app_factory.py +88 -17
  50. package/latticeai/brain/_kg_common.py +1123 -0
  51. package/latticeai/brain/discovery.py +1455 -0
  52. package/latticeai/brain/documents.py +218 -0
  53. package/latticeai/brain/ingest.py +644 -0
  54. package/latticeai/brain/projection.py +561 -0
  55. package/latticeai/brain/provenance.py +401 -0
  56. package/latticeai/brain/retrieval.py +1316 -0
  57. package/latticeai/brain/schema.py +640 -0
  58. package/latticeai/brain/store.py +216 -0
  59. package/latticeai/brain/write_master.py +225 -0
  60. package/latticeai/core/invitations.py +131 -0
  61. package/latticeai/core/marketplace.py +1 -1
  62. package/latticeai/core/multi_agent.py +1 -1
  63. package/latticeai/core/policy.py +54 -0
  64. package/latticeai/core/realtime.py +65 -44
  65. package/latticeai/core/sessions.py +31 -5
  66. package/latticeai/core/users.py +147 -0
  67. package/latticeai/core/workspace_os.py +420 -20
  68. package/latticeai/services/agent_runtime.py +242 -4
  69. package/latticeai/services/run_executor.py +328 -0
  70. package/latticeai/services/workspace_service.py +27 -19
  71. package/package.json +54 -27
  72. package/scripts/build_frontend_assets.mjs +38 -0
  73. package/scripts/bump_version.py +1 -1
  74. package/scripts/export_openapi.py +31 -0
  75. package/scripts/lint_frontend.mjs +86 -0
  76. package/scripts/run_python.mjs +47 -0
  77. package/src-tauri/Cargo.lock +4833 -0
  78. package/src-tauri/Cargo.toml +19 -0
  79. package/src-tauri/build.rs +3 -0
  80. package/src-tauri/capabilities/default.json +7 -0
  81. package/src-tauri/src/main.rs +78 -0
  82. package/src-tauri/tauri.conf.json +36 -0
  83. package/static/app/asset-manifest.json +32 -0
  84. package/static/app/assets/core-CwxXejkd.js +2 -0
  85. package/static/app/assets/core-CwxXejkd.js.map +1 -0
  86. package/static/app/assets/index-CJRAzNnf.js +333 -0
  87. package/static/app/assets/index-CJRAzNnf.js.map +1 -0
  88. package/static/app/assets/index-CSwBBgf4.css +2 -0
  89. package/static/app/index.html +25 -0
  90. package/static/manifest.json +2 -2
  91. package/static/sw.js +4 -4
  92. package/scripts/build_v3_assets.mjs +0 -170
  93. package/scripts/lint_v3.mjs +0 -97
  94. package/static/account.html +0 -113
  95. package/static/activity.html +0 -73
  96. package/static/admin.html +0 -486
  97. package/static/agents.html +0 -139
  98. package/static/chat.html +0 -841
  99. package/static/css/reference/account.css +0 -439
  100. package/static/css/reference/admin.css +0 -610
  101. package/static/css/reference/base.css +0 -1661
  102. package/static/css/reference/chat.css +0 -4623
  103. package/static/css/reference/graph.css +0 -1016
  104. package/static/css/responsive.css +0 -861
  105. package/static/graph.html +0 -122
  106. package/static/platform.css +0 -104
  107. package/static/plugins.html +0 -136
  108. package/static/scripts/account.js +0 -238
  109. package/static/scripts/admin.js +0 -1614
  110. package/static/scripts/chat.js +0 -5081
  111. package/static/scripts/graph.js +0 -1804
  112. package/static/scripts/platform.js +0 -64
  113. package/static/scripts/ux.js +0 -167
  114. package/static/scripts/workspace.js +0 -948
  115. package/static/v3/asset-manifest.json +0 -56
  116. package/static/v3/css/lattice.base.49deefb5.css +0 -128
  117. package/static/v3/css/lattice.base.css +0 -128
  118. package/static/v3/css/lattice.components.cde18231.css +0 -472
  119. package/static/v3/css/lattice.components.css +0 -472
  120. package/static/v3/css/lattice.shell.29d36d85.css +0 -452
  121. package/static/v3/css/lattice.shell.css +0 -452
  122. package/static/v3/css/lattice.tokens.304cbc40.css +0 -135
  123. package/static/v3/css/lattice.tokens.css +0 -135
  124. package/static/v3/css/lattice.views.0a18b6c5.css +0 -360
  125. package/static/v3/css/lattice.views.css +0 -360
  126. package/static/v3/index.html +0 -68
  127. package/static/v3/js/app.356e6452.js +0 -26
  128. package/static/v3/js/app.js +0 -26
  129. package/static/v3/js/core/api.7a308b89.js +0 -568
  130. package/static/v3/js/core/api.js +0 -568
  131. package/static/v3/js/core/components.f25b3b93.js +0 -230
  132. package/static/v3/js/core/components.js +0 -230
  133. package/static/v3/js/core/dom.a2773eb0.js +0 -148
  134. package/static/v3/js/core/dom.js +0 -148
  135. package/static/v3/js/core/router.584570f2.js +0 -37
  136. package/static/v3/js/core/router.js +0 -37
  137. package/static/v3/js/core/routes.7222343d.js +0 -93
  138. package/static/v3/js/core/routes.js +0 -93
  139. package/static/v3/js/core/shell.a1657f20.js +0 -391
  140. package/static/v3/js/core/shell.js +0 -391
  141. package/static/v3/js/core/store.204a08b2.js +0 -113
  142. package/static/v3/js/core/store.js +0 -113
  143. package/static/v3/js/views/admin-audit.660a1fb1.js +0 -185
  144. package/static/v3/js/views/admin-audit.js +0 -185
  145. package/static/v3/js/views/admin-permissions.a7ae5f09.js +0 -177
  146. package/static/v3/js/views/admin-permissions.js +0 -177
  147. package/static/v3/js/views/admin-policies.3658fd86.js +0 -102
  148. package/static/v3/js/views/admin-policies.js +0 -102
  149. package/static/v3/js/views/admin-private-vpc.7d342d36.js +0 -135
  150. package/static/v3/js/views/admin-private-vpc.js +0 -135
  151. package/static/v3/js/views/admin-security.07c66b72.js +0 -180
  152. package/static/v3/js/views/admin-security.js +0 -180
  153. package/static/v3/js/views/admin-users.03bac88c.js +0 -168
  154. package/static/v3/js/views/admin-users.js +0 -168
  155. package/static/v3/js/views/agents.014d0b74.js +0 -541
  156. package/static/v3/js/views/agents.js +0 -541
  157. package/static/v3/js/views/chat.e6dd7dd0.js +0 -601
  158. package/static/v3/js/views/chat.js +0 -601
  159. package/static/v3/js/views/files.adad14c1.js +0 -365
  160. package/static/v3/js/views/files.js +0 -365
  161. package/static/v3/js/views/graph-canvas.17c15d65.js +0 -509
  162. package/static/v3/js/views/graph-canvas.js +0 -509
  163. package/static/v3/js/views/home.24f8b8ae.js +0 -200
  164. package/static/v3/js/views/home.js +0 -200
  165. package/static/v3/js/views/hooks.37895880.js +0 -220
  166. package/static/v3/js/views/hooks.js +0 -220
  167. package/static/v3/js/views/hybrid-search.2fb63ed9.js +0 -194
  168. package/static/v3/js/views/hybrid-search.js +0 -194
  169. package/static/v3/js/views/knowledge-graph.5e40cbeb.js +0 -509
  170. package/static/v3/js/views/knowledge-graph.js +0 -509
  171. package/static/v3/js/views/marketplace.ab0583d4.js +0 -141
  172. package/static/v3/js/views/marketplace.js +0 -141
  173. package/static/v3/js/views/mcp.99b5c6a7.js +0 -114
  174. package/static/v3/js/views/mcp.js +0 -114
  175. package/static/v3/js/views/memory.4ebdf474.js +0 -147
  176. package/static/v3/js/views/memory.js +0 -147
  177. package/static/v3/js/views/models.a1ffa147.js +0 -256
  178. package/static/v3/js/views/models.js +0 -256
  179. package/static/v3/js/views/my-computer.d9d9ae1c.js +0 -463
  180. package/static/v3/js/views/my-computer.js +0 -463
  181. package/static/v3/js/views/pipeline.c522f1ce.js +0 -157
  182. package/static/v3/js/views/pipeline.js +0 -157
  183. package/static/v3/js/views/planning.9ac3e313.js +0 -153
  184. package/static/v3/js/views/planning.js +0 -153
  185. package/static/v3/js/views/settings.8631fa5e.js +0 -318
  186. package/static/v3/js/views/settings.js +0 -318
  187. package/static/v3/js/views/skills.c6c2f965.js +0 -109
  188. package/static/v3/js/views/skills.js +0 -109
  189. package/static/v3/js/views/tools.e4f11276.js +0 -108
  190. package/static/v3/js/views/tools.js +0 -108
  191. package/static/v3/js/views/workflows.26c57290.js +0 -128
  192. package/static/v3/js/views/workflows.js +0 -128
  193. package/static/workflows.html +0 -146
  194. package/static/workspace.css +0 -1121
  195. package/static/workspace.html +0 -357
@@ -0,0 +1,644 @@
1
+ from __future__ import annotations
2
+
3
+ # ruff: noqa: F403,F405
4
+
5
+ from ._kg_common import * # noqa: F403,F401
6
+
7
+
8
+ class KnowledgeGraphIngestMixin:
9
+ def ingest_message(
10
+ self,
11
+ role: str,
12
+ content: str,
13
+ *,
14
+ user_email: Optional[str] = None,
15
+ user_nickname: Optional[str] = None,
16
+ source: Optional[str] = None,
17
+ conversation_id: Optional[str] = None,
18
+ raw: Optional[Dict[str, Any]] = None,
19
+ ) -> Dict[str, Any]:
20
+ content = str(content or "")
21
+ digest = _sha256_text(
22
+ "|".join([role or "", content, conversation_id or "", user_email or ""])
23
+ )[:24]
24
+ node_type = "AIResponse" if role == "assistant" else "Message"
25
+ node_id = f"{node_type.lower()}:{digest}"
26
+ conv_id = f"conversation:{_slug(conversation_id or 'default')}"
27
+ metadata = {
28
+ "role": role,
29
+ "source": source,
30
+ "conversation_id": conversation_id,
31
+ "user_email": user_email,
32
+ "user_nickname": user_nickname,
33
+ "chars": len(content),
34
+ }
35
+ concepts = _extract_concepts(content)
36
+ triples = _extract_triples(content, concepts)
37
+ semantic = _semantic_items(content)
38
+
39
+ with self._connect() as conn:
40
+ # ── 1. Chat node (점: 명사 — 대화 세션 단위) ─────────────────────
41
+ # One Chat node per conversation_id; title = first 80 chars of
42
+ # the first user message in this session (updated on each call).
43
+ chat_title = _clean_text(content)[:80] or (conversation_id or "대화")
44
+ self._upsert_node(
45
+ conn,
46
+ conv_id,
47
+ "Chat",
48
+ chat_title,
49
+ summary=_clean_text(content)[:400],
50
+ metadata={"source": source, "conversation_id": conversation_id},
51
+ )
52
+
53
+ # ── 2. Person node (점: 명사 — 사람) ─────────────────────────────
54
+ person_id = None
55
+ if user_email or user_nickname:
56
+ person_key = user_email or user_nickname or "unknown"
57
+ person_id = f"person:{_slug(person_key)}"
58
+ self._upsert_node(
59
+ conn,
60
+ person_id,
61
+ "Person",
62
+ user_nickname or user_email or "Unknown",
63
+ metadata={"email": user_email, "nickname": user_nickname},
64
+ )
65
+ # 선: 동사 — Person이 Chat을 "작성함"
66
+ self._upsert_edge(
67
+ conn,
68
+ person_id,
69
+ conv_id,
70
+ "작성함",
71
+ weight=1.0,
72
+ metadata={"role": role},
73
+ )
74
+
75
+ # ── 3. Raw message node (RAG 검색용, 그래프에서 숨김) ─────────────
76
+ self._upsert_node(
77
+ conn,
78
+ node_id,
79
+ node_type,
80
+ _clean_text(content)[:80] or role,
81
+ summary=_clean_text(content)[:500],
82
+ metadata=metadata,
83
+ raw=raw or metadata,
84
+ )
85
+ # 선: Chat이 메시지를 "포함함"
86
+ self._upsert_edge(
87
+ conn, conv_id, node_id, "포함함", weight=0.3, metadata={"role": role}
88
+ )
89
+
90
+ # ── 4. RAG chunks (검색용, 그래프에서 숨김) ──────────────────────
91
+ for index, chunk in enumerate(_chunks(content)):
92
+ chunk_id = f"chunk:{_sha256_text(f'{node_id}:{index}:{chunk}')[:24]}"
93
+ self._upsert_node(
94
+ conn,
95
+ chunk_id,
96
+ "Chunk",
97
+ f"chunk {index + 1}",
98
+ summary=chunk[:500],
99
+ metadata={"index": index, "source_node": node_id},
100
+ )
101
+ self._upsert_chunk(
102
+ conn,
103
+ chunk_id=chunk_id,
104
+ source_node=node_id,
105
+ text=chunk,
106
+ metadata={"index": index, "source_node": node_id},
107
+ )
108
+ self._upsert_edge(conn, node_id, chunk_id, "포함함")
109
+
110
+ # ── 5. Concept / Feature / Error / Code 노드 (점: 명사) ───────────
111
+ concept_ids: Dict[str, str] = {}
112
+ for concept in concepts:
113
+ node_t = _classify_node_type(concept, content)
114
+ cid = f"{node_t.lower()}:{_slug(concept)}"
115
+ concept_ids[concept.lower()] = cid
116
+ self._upsert_node(
117
+ conn,
118
+ cid,
119
+ node_t,
120
+ concept,
121
+ metadata={"auto_extracted": True, "source": source},
122
+ )
123
+ # 선: Chat이 개념을 "언급함"
124
+ self._upsert_edge(
125
+ conn,
126
+ conv_id,
127
+ cid,
128
+ "언급함",
129
+ weight=0.7,
130
+ metadata={"source": source},
131
+ )
132
+
133
+ # ── 6. Concept–Concept 엣지 (선: 동사형) ─────────────────────────
134
+ for triple in triples:
135
+ subj_id = concept_ids.get(triple["subject"].lower())
136
+ obj_id = concept_ids.get(triple["object"].lower())
137
+ if subj_id and obj_id and subj_id != obj_id:
138
+ self._upsert_edge(
139
+ conn,
140
+ subj_id,
141
+ obj_id,
142
+ triple["relation"], # 동사형 레이블
143
+ weight=1.0,
144
+ metadata={"context": triple.get("context", "")[:240]},
145
+ )
146
+
147
+ # ── 7. Task / Decision 노드 (점: 명사) ────────────────────────────
148
+ for item in semantic:
149
+ sem_type = item["type"]
150
+ sem_title = item["title"]
151
+ sem_id = f"{sem_type.lower()}:{_sha256_text(f'{conv_id}:{sem_type}:{sem_title}')[:24]}"
152
+ self._upsert_node(
153
+ conn,
154
+ sem_id,
155
+ sem_type,
156
+ sem_title,
157
+ summary=item["summary"],
158
+ metadata={"auto_extracted": True, "source_node": node_id},
159
+ raw=item,
160
+ )
161
+ # 선: Chat이 Task/Decision을 "생성함"
162
+ self._upsert_edge(conn, conv_id, sem_id, "생성함", weight=0.9)
163
+ # Task/Decision이 관련 개념을 "언급함"
164
+ for cid in list(concept_ids.values())[:3]:
165
+ self._upsert_edge(conn, sem_id, cid, "언급함", weight=0.6)
166
+
167
+ return {"node_id": node_id, "type": node_type}
168
+
169
+ def ingest_document(
170
+ self,
171
+ path: Path,
172
+ *,
173
+ original_filename: Optional[str] = None,
174
+ mime_type: Optional[str] = None,
175
+ uploader: Optional[str] = None,
176
+ conversation_id: Optional[str] = None,
177
+ extracted: Optional[Dict[str, Any]] = None,
178
+ source_type: Optional[str] = None,
179
+ source_uri: Optional[str] = None,
180
+ captured_at: Optional[str] = None,
181
+ modified_at: Optional[str] = None,
182
+ owner: Optional[str] = None,
183
+ workspace_id: Optional[str] = None,
184
+ permissions: Optional[Dict[str, Any]] = None,
185
+ ) -> Dict[str, Any]:
186
+ path = Path(path)
187
+ data = path.read_bytes()
188
+ digest = _sha256_bytes(data)
189
+ ext = path.suffix.lower()
190
+ filename = original_filename or path.name
191
+ captured_at = captured_at or _now()
192
+ blob_path = self.blob_dir / digest[:2] / f"{digest}{ext}"
193
+ blob_path.parent.mkdir(parents=True, exist_ok=True)
194
+ if not blob_path.exists():
195
+ shutil.copyfile(path, blob_path)
196
+
197
+ doc_meta = self._document_structure(path, ext)
198
+ text = str(
199
+ (extracted or {}).get("content") or (extracted or {}).get("preview") or ""
200
+ )
201
+ file_id = f"file:{digest[:24]}"
202
+ metadata = {
203
+ "filename": filename,
204
+ "ext": ext,
205
+ "mime_type": mime_type,
206
+ "bytes": len(data),
207
+ "sha256": digest,
208
+ "content_hash": digest,
209
+ "blob_path": str(blob_path),
210
+ "uploader": uploader,
211
+ "owner": owner or uploader,
212
+ "workspace_id": workspace_id,
213
+ "permissions": permissions or {},
214
+ "source_type": source_type or "file",
215
+ "source_uri": source_uri or str(path),
216
+ "captured_at": captured_at,
217
+ "modified_at": modified_at,
218
+ "conversation_id": conversation_id,
219
+ "extracted": {k: v for k, v in (extracted or {}).items() if k != "content"},
220
+ "structure": doc_meta,
221
+ }
222
+ full_text = f"{filename}\n{text}"
223
+ concepts = _extract_concepts(full_text, limit=15)
224
+ triples = _extract_triples(full_text, concepts)
225
+ chunk_ids: List[str] = []
226
+ source_node_id: Optional[str] = None
227
+
228
+ with self._connect() as conn:
229
+ duplicate = self._node_exists(conn, file_id)
230
+ # ── Document 노드 (점: 명사 — 파일) ────────────────────────────────
231
+ self._upsert_node(
232
+ conn,
233
+ file_id,
234
+ "Document",
235
+ filename,
236
+ summary=(text or filename)[:500],
237
+ metadata=metadata,
238
+ raw=metadata,
239
+ )
240
+ self._ingest_structure_nodes(conn, file_id, filename, doc_meta)
241
+
242
+ # ── SOURCE 노드 + indexed_from (v3.6.0, source_type 지정 시) ──────
243
+ if source_type:
244
+ source_node_id = self._attach_source_node(
245
+ conn,
246
+ file_id,
247
+ source_type=source_type,
248
+ source_uri=source_uri or str(path),
249
+ title=filename,
250
+ content_hash=digest,
251
+ captured_at=captured_at,
252
+ extra={
253
+ "owner": owner or uploader,
254
+ "workspace_id": workspace_id,
255
+ "ext": ext,
256
+ },
257
+ )
258
+
259
+ # ── Person 노드 + 동사형 엣지 ─────────────────────────────────────
260
+ if uploader:
261
+ person_id = f"person:{_slug(uploader)}"
262
+ self._upsert_node(
263
+ conn,
264
+ person_id,
265
+ "Person",
266
+ uploader,
267
+ metadata={"email": uploader},
268
+ )
269
+ # 선: 동사 — Person이 Document를 "업로드함"
270
+ self._upsert_edge(conn, person_id, file_id, "업로드함", weight=1.0)
271
+
272
+ # ── Chat 노드와 연결 ──────────────────────────────────────────────
273
+ if conversation_id:
274
+ conv_id = f"conversation:{_slug(conversation_id)}"
275
+ self._upsert_node(conn, conv_id, "Chat", conversation_id)
276
+ # 선: 동사 — Chat이 Document를 "언급함"
277
+ self._upsert_edge(conn, conv_id, file_id, "언급함", weight=0.8)
278
+
279
+ # ── RAG chunks (검색용, 그래프 비표시) ────────────────────────────
280
+ for index, chunk in enumerate(_chunks(text)):
281
+ chunk_id = f"chunk:{_sha256_text(f'{file_id}:{index}:{chunk}')[:24]}"
282
+ chunk_ids.append(chunk_id)
283
+ self._upsert_node(
284
+ conn,
285
+ chunk_id,
286
+ "Chunk",
287
+ f"{filename} chunk {index + 1}",
288
+ summary=chunk[:500],
289
+ metadata={"index": index, "source_node": file_id},
290
+ )
291
+ self._upsert_chunk(
292
+ conn,
293
+ chunk_id=chunk_id,
294
+ source_node=file_id,
295
+ text=chunk,
296
+ metadata={"index": index, "source_node": file_id},
297
+ )
298
+ self._upsert_edge(conn, file_id, chunk_id, "포함함")
299
+
300
+ # ── Concept / Feature / Error / Code 노드 + 동사형 엣지 ───────────
301
+ concept_ids: Dict[str, str] = {}
302
+ for concept in concepts:
303
+ node_t = _classify_node_type(concept, full_text)
304
+ cid = f"{node_t.lower()}:{_slug(concept)}"
305
+ concept_ids[concept.lower()] = cid
306
+ self._upsert_node(
307
+ conn,
308
+ cid,
309
+ node_t,
310
+ concept,
311
+ metadata={"auto_extracted": True, "source_file": filename},
312
+ )
313
+ # 선: 동사 — Document가 Concept을 "포함함"
314
+ self._upsert_edge(conn, file_id, cid, "포함함", weight=0.8)
315
+
316
+ # ── Concept–Concept 엣지 (선: 동사형) ───────────────────────────
317
+ for triple in triples:
318
+ subj_id = concept_ids.get(triple["subject"].lower())
319
+ obj_id = concept_ids.get(triple["object"].lower())
320
+ if subj_id and obj_id and subj_id != obj_id:
321
+ self._upsert_edge(
322
+ conn,
323
+ subj_id,
324
+ obj_id,
325
+ triple["relation"],
326
+ weight=1.0,
327
+ metadata={"context": triple.get("context", "")[:240]},
328
+ )
329
+
330
+ # ── Task / Decision 노드 ──────────────────────────────────────────
331
+ for item in _semantic_items(text):
332
+ sem_type = item["type"]
333
+ sem_title = item["title"]
334
+ sem_id = f"{sem_type.lower()}:{_sha256_text(f'{file_id}:{sem_type}:{sem_title}')[:24]}"
335
+ self._upsert_node(
336
+ conn,
337
+ sem_id,
338
+ sem_type,
339
+ sem_title,
340
+ summary=item["summary"],
341
+ metadata={
342
+ "auto_extracted": True,
343
+ "source_node": file_id,
344
+ "filename": filename,
345
+ },
346
+ raw=item,
347
+ )
348
+ # 선: Document가 Task/Decision을 "포함함"
349
+ self._upsert_edge(conn, file_id, sem_id, "포함함", weight=0.9)
350
+
351
+ return {
352
+ "node_id": file_id,
353
+ "type": "Document",
354
+ "sha256": digest,
355
+ "content_hash": digest,
356
+ "source_node_id": source_node_id,
357
+ "chunk_ids": chunk_ids,
358
+ "chunk_count": len(chunk_ids),
359
+ "duplicate": duplicate,
360
+ "captured_at": captured_at,
361
+ "metadata": metadata,
362
+ }
363
+
364
+ def ingest_event(
365
+ self,
366
+ event_type: str,
367
+ title: str,
368
+ *,
369
+ user_email: Optional[str] = None,
370
+ user_nickname: Optional[str] = None,
371
+ source: Optional[str] = None,
372
+ conversation_id: Optional[str] = None,
373
+ metadata: Optional[Dict[str, Any]] = None,
374
+ ) -> Dict[str, Any]:
375
+ event_type = str(event_type or "Event")
376
+ title = str(title or event_type)
377
+ payload = {
378
+ "event_type": event_type,
379
+ "title": title,
380
+ "user_email": user_email,
381
+ "user_nickname": user_nickname,
382
+ "source": source,
383
+ "conversation_id": conversation_id,
384
+ "metadata": metadata or {},
385
+ "timestamp": _now(),
386
+ }
387
+ event_id = f"event:{_sha256_text(_json(payload))[:24]}"
388
+ conv_id = f"conversation:{_slug(conversation_id or 'default')}"
389
+ with self._connect() as conn:
390
+ self._upsert_node(
391
+ conn,
392
+ event_id,
393
+ event_type,
394
+ title,
395
+ summary=title,
396
+ metadata=payload,
397
+ raw=payload,
398
+ )
399
+ self._upsert_node(
400
+ conn,
401
+ conv_id,
402
+ "Conversation",
403
+ conversation_id or "Default conversation",
404
+ metadata={"source": source},
405
+ )
406
+ self._upsert_edge(
407
+ conn, conv_id, event_id, "has_event", metadata={"source": source}
408
+ )
409
+ if user_email or user_nickname:
410
+ person_key = user_email or user_nickname or "unknown"
411
+ person_id = f"person:{_slug(person_key)}"
412
+ self._upsert_node(
413
+ conn,
414
+ person_id,
415
+ "Person",
416
+ user_nickname or user_email or "Unknown user",
417
+ metadata={"email": user_email},
418
+ )
419
+ self._upsert_edge(
420
+ conn,
421
+ person_id,
422
+ event_id,
423
+ "triggered",
424
+ metadata={"event_type": event_type},
425
+ )
426
+ return {"node_id": event_id, "type": event_type}
427
+
428
+ def _node_exists(self, conn: sqlite3.Connection, node_id: str) -> bool:
429
+ row = conn.execute("SELECT 1 FROM nodes WHERE id = ?", (node_id,)).fetchone()
430
+ return row is not None
431
+
432
+ def node_is_embedded(self, node_id: str) -> bool:
433
+ """True when a vector embedding exists for ``node_id`` (RAG-ready)."""
434
+ with self._connect() as conn:
435
+ row = conn.execute(
436
+ "SELECT 1 FROM vector_embeddings WHERE item_id = ? LIMIT 1",
437
+ (node_id,),
438
+ ).fetchone()
439
+ return row is not None
440
+
441
+ def _attach_source_node(
442
+ self,
443
+ conn: sqlite3.Connection,
444
+ content_node_id: str,
445
+ *,
446
+ source_type: str,
447
+ source_uri: Optional[str] = None,
448
+ title: Optional[str] = None,
449
+ content_hash: Optional[str] = None,
450
+ captured_at: Optional[str] = None,
451
+ extra: Optional[Dict[str, Any]] = None,
452
+ ) -> str:
453
+ """Create the SOURCE node for an ingested item and link it via INDEXED_FROM.
454
+
455
+ Every ingested content node points at exactly one SOURCE node, so the
456
+ graph is always able to explain *where* a node came from. The source id
457
+ is derived from (source_type, source_uri | content_hash) so re-ingesting
458
+ the same origin reuses the same SOURCE node (idempotent).
459
+ """
460
+ key = source_uri or content_hash or content_node_id
461
+ source_id = f"source:{_sha256_text(f'{source_type}|{key}')[:24]}"
462
+ meta = {
463
+ "source_type": source_type,
464
+ "source_uri": source_uri,
465
+ "content_hash": content_hash,
466
+ "captured_at": captured_at or _now(),
467
+ **(extra or {}),
468
+ }
469
+ label = title or source_uri or source_type
470
+ self._upsert_node(
471
+ conn,
472
+ source_id,
473
+ "Source",
474
+ label,
475
+ summary=str(source_uri or title or source_type)[:400],
476
+ metadata=meta,
477
+ )
478
+ # 선: 콘텐츠 노드가 "이 출처에서 색인됨" (indexed_from → SOURCE)
479
+ self._upsert_edge(
480
+ conn,
481
+ content_node_id,
482
+ source_id,
483
+ "indexed_from",
484
+ weight=1.0,
485
+ metadata={"source_type": source_type},
486
+ )
487
+ return source_id
488
+
489
+ def ingest_source(
490
+ self,
491
+ *,
492
+ source_type: str,
493
+ title: str,
494
+ text: str,
495
+ source_uri: Optional[str] = None,
496
+ owner: Optional[str] = None,
497
+ workspace_id: Optional[str] = None,
498
+ permissions: Optional[Dict[str, Any]] = None,
499
+ captured_at: Optional[str] = None,
500
+ modified_at: Optional[str] = None,
501
+ conversation_id: Optional[str] = None,
502
+ metadata: Optional[Dict[str, Any]] = None,
503
+ ) -> Dict[str, Any]:
504
+ """Unified text/web ingestion: one shape for URL, browser tab, note, text.
505
+
506
+ Creates a content ``Document`` node (idempotent by content hash), a
507
+ ``Source`` node linked via ``indexed_from``, RAG chunks, and extracted
508
+ Concept/Task/Decision nodes — mirroring ingest_document for non-file
509
+ sources. Returns the full set of ids the caller needs to record
510
+ provenance, including ``duplicate`` (was the content already indexed).
511
+ """
512
+ source_type = str(source_type or "text")
513
+ text = str(text or "")
514
+ title = (
515
+ _clean_text(str(title or source_uri or source_type))[:240] or source_type
516
+ )
517
+ captured_at = captured_at or _now()
518
+ content_hash = _sha256_text(f"{source_type}|{source_uri or ''}|{text}")
519
+ content_id = f"webdoc:{content_hash[:24]}"
520
+ full_text = f"{title}\n{text}"
521
+ node_meta = {
522
+ "source_type": source_type,
523
+ "source_uri": source_uri,
524
+ "content_hash": content_hash,
525
+ "title": title,
526
+ "captured_at": captured_at,
527
+ "modified_at": modified_at,
528
+ "owner": owner,
529
+ "workspace_id": workspace_id,
530
+ "permissions": permissions or {},
531
+ "chars": len(text),
532
+ **(metadata or {}),
533
+ }
534
+ concepts = _extract_concepts(full_text, limit=15)
535
+ triples = _extract_triples(full_text, concepts)
536
+ chunk_ids: List[str] = []
537
+
538
+ with self._connect() as conn:
539
+ duplicate = self._node_exists(conn, content_id)
540
+ # ── 콘텐츠 노드 (점: 명사 — 문서) ────────────────────────────────
541
+ self._upsert_node(
542
+ conn,
543
+ content_id,
544
+ "Document",
545
+ title,
546
+ summary=(text or title)[:500],
547
+ metadata=node_meta,
548
+ raw=node_meta,
549
+ )
550
+ # ── SOURCE 노드 + indexed_from 엣지 (출처 추적) ──────────────────
551
+ source_node_id = self._attach_source_node(
552
+ conn,
553
+ content_id,
554
+ source_type=source_type,
555
+ source_uri=source_uri,
556
+ title=title,
557
+ content_hash=content_hash,
558
+ captured_at=captured_at,
559
+ extra={"owner": owner, "workspace_id": workspace_id},
560
+ )
561
+ # ── 소유자(Person) + 동사형 엣지 ────────────────────────────────
562
+ if owner:
563
+ person_id = f"person:{_slug(owner)}"
564
+ self._upsert_node(
565
+ conn, person_id, "Person", owner, metadata={"email": owner}
566
+ )
567
+ self._upsert_edge(conn, person_id, content_id, "업로드함", weight=1.0)
568
+ # ── 대화 연결 ───────────────────────────────────────────────────
569
+ if conversation_id:
570
+ conv_id = f"conversation:{_slug(conversation_id)}"
571
+ self._upsert_node(conn, conv_id, "Chat", conversation_id)
572
+ self._upsert_edge(conn, conv_id, content_id, "언급함", weight=0.8)
573
+ # ── RAG 청크 ────────────────────────────────────────────────────
574
+ for index, chunk in enumerate(_chunks(text)):
575
+ chunk_id = f"chunk:{_sha256_text(f'{content_id}:{index}:{chunk}')[:24]}"
576
+ chunk_ids.append(chunk_id)
577
+ self._upsert_node(
578
+ conn,
579
+ chunk_id,
580
+ "Chunk",
581
+ f"{title} chunk {index + 1}",
582
+ summary=chunk[:500],
583
+ metadata={"index": index, "source_node": content_id},
584
+ )
585
+ self._upsert_chunk(
586
+ conn,
587
+ chunk_id=chunk_id,
588
+ source_node=content_id,
589
+ text=chunk,
590
+ metadata={"index": index, "source_node": content_id},
591
+ )
592
+ self._upsert_edge(conn, content_id, chunk_id, "포함함")
593
+ # ── Concept / Feature / Error / Code 노드 + 엣지 ────────────────
594
+ concept_ids: Dict[str, str] = {}
595
+ for concept in concepts:
596
+ node_t = _classify_node_type(concept, full_text)
597
+ cid = f"{node_t.lower()}:{_slug(concept)}"
598
+ concept_ids[concept.lower()] = cid
599
+ self._upsert_node(
600
+ conn,
601
+ cid,
602
+ node_t,
603
+ concept,
604
+ metadata={"auto_extracted": True, "source_type": source_type},
605
+ )
606
+ self._upsert_edge(conn, content_id, cid, "포함함", weight=0.8)
607
+ for triple in triples:
608
+ subj_id = concept_ids.get(triple["subject"].lower())
609
+ obj_id = concept_ids.get(triple["object"].lower())
610
+ if subj_id and obj_id and subj_id != obj_id:
611
+ self._upsert_edge(
612
+ conn,
613
+ subj_id,
614
+ obj_id,
615
+ triple["relation"],
616
+ weight=1.0,
617
+ metadata={"context": triple.get("context", "")[:240]},
618
+ )
619
+ # ── Task / Decision 노드 ────────────────────────────────────────
620
+ for item in _semantic_items(text):
621
+ sem_type = item["type"]
622
+ sem_title = item["title"]
623
+ sem_id = f"{sem_type.lower()}:{_sha256_text(f'{content_id}:{sem_type}:{sem_title}')[:24]}"
624
+ self._upsert_node(
625
+ conn,
626
+ sem_id,
627
+ sem_type,
628
+ sem_title,
629
+ summary=item["summary"],
630
+ metadata={"auto_extracted": True, "source_node": content_id},
631
+ raw=item,
632
+ )
633
+ self._upsert_edge(conn, content_id, sem_id, "포함함", weight=0.9)
634
+
635
+ return {
636
+ "node_id": content_id,
637
+ "type": "Document",
638
+ "source_node_id": source_node_id,
639
+ "content_hash": content_hash,
640
+ "chunk_ids": chunk_ids,
641
+ "chunk_count": len(chunk_ids),
642
+ "duplicate": duplicate,
643
+ "captured_at": captured_at,
644
+ }