feat(agent): persist RAG and PlayBook trust receipts
Some checks failed
CD Pipeline / workflow-shape (push) Successful in 1s
CD Pipeline / cancel-stale-cd (push) Has been skipped
CD Pipeline / build-and-deploy (push) Has been cancelled
CD Pipeline / post-deploy-checks (push) Has been cancelled
CD Pipeline / tests (push) Has been cancelled

This commit is contained in:
ogt
2026-07-10 20:19:51 +08:00
parent 7079b1e0ec
commit 0bb5e9c95a
4 changed files with 558 additions and 119 deletions

View File

@@ -680,18 +680,44 @@ class KnowledgeService:
async def _embed_entry(self, entry_id: str, title: str, content: str) -> None:
"""背景任務:產生並儲存 embedding"""
await self.ensure_entry_embedding(
entry_id,
title,
content,
)
async def ensure_entry_embedding(
self,
entry_id: str,
title: str,
content: str,
*,
project_id: str | None = None,
) -> bool:
"""Synchronously persist and acknowledge one KM embedding."""
try:
text = f"search_document: {title}\n\n{content[:2000]}"
embedding = await self._embed_svc.embed_text(text)
document = f"search_document: {title}\n\n{content[:2000]}"
embedding = await self._embed_svc.embed_text(document)
if not embedding:
logger.warning("knowledge_embedding_empty", entry_id=entry_id)
return
async with get_db_context() as db:
return False
context = (
get_db_context(project_id)
if project_id is not None
else get_db_context()
)
async with context as db:
repo = KnowledgeDBRepository(db)
await repo.save_embedding(entry_id, embedding)
saved = await repo.save_embedding(entry_id, embedding)
if not saved:
logger.warning("knowledge_embedding_not_saved", entry_id=entry_id)
return False
logger.info("knowledge_embedding_saved", entry_id=entry_id)
return True
except Exception as e:
logger.warning("knowledge_embedding_failed", entry_id=entry_id, error=str(e))
return False
async def get_entry(self, entry_id: str) -> KnowledgeEntry | None:
"""取得知識條目 (view_count +1)"""