fix КФП

This commit is contained in:
2026-06-16 10:07:06 +03:00
parent b1506f8695
commit 70910b82d2
3 changed files with 47 additions and 14 deletions
+21 -3
View File
@@ -1,5 +1,6 @@
import asyncio
import json
import logging
import threading
from concurrent.futures import ThreadPoolExecutor
from datetime import datetime, timezone
@@ -20,6 +21,9 @@ DEFAULT_PROFILE: dict[str, Any] = {
}
logger = logging.getLogger(__name__)
class MemoryService:
def __init__(self, db: Session, user_id: int):
self.db = db
@@ -38,10 +42,24 @@ class MemoryService:
@staticmethod
def _schedule_rag(coro) -> None:
def runner() -> None:
asyncio.run(coro)
try:
asyncio.run(coro)
except Exception:
logger.exception("RAG background task failed")
threading.Thread(target=runner, daemon=True).start()
@staticmethod
def _rag_fact_payload(fact: MemoryFact) -> dict[str, Any]:
return {
"fact_id": int(fact.id),
"user_id": int(fact.user_id),
"content": fact.content,
"category": fact.category,
"importance": int(fact.importance),
"active": bool(fact.active),
}
def get_profile(self) -> dict[str, Any]:
row = self.db.scalar(select(UserProfile).where(UserProfile.user_id == self.user_id).limit(1))
if not row:
@@ -114,7 +132,7 @@ class MemoryService:
self.db.commit()
from app.rag.ingest import index_memory_fact
self._schedule_rag(index_memory_fact(existing))
self._schedule_rag(index_memory_fact(**self._rag_fact_payload(existing)))
result = {
"ok": True,
"action": "updated",
@@ -139,7 +157,7 @@ class MemoryService:
self.db.refresh(fact)
from app.rag.ingest import index_memory_fact
self._schedule_rag(index_memory_fact(fact))
self._schedule_rag(index_memory_fact(**self._rag_fact_payload(fact)))
result = {
"ok": True,
"action": "created",