forked from BasedHardware/omi
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcanonical_vector_sync.py
More file actions
75 lines (64 loc) · 2.4 KB
/
Copy pathcanonical_vector_sync.py
File metadata and controls
75 lines (64 loc) · 2.4 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
"""Sync universal canonical memory items to Pinecone using user-scoped provider ids."""
from __future__ import annotations
import logging
from typing import Any, Callable, Optional
from models.memory_evidence import SourceState
from models.product_memory import (
RESTRICTED_SENSITIVITY_LABELS,
MemoryItem,
MemoryItemStatus,
ProcessingState,
)
logger = logging.getLogger(__name__)
def delete_canonical_memory_vector(uid: str, memory_id: str) -> bool:
"""Delete all provider identities for one user's canonical memory."""
try:
from database.vector_db import delete_canonical_memory_vectors
return delete_canonical_memory_vectors(uid, memory_id)
except Exception:
logger.exception(
"canonical vector delete failed memory_id=%s uid=%s",
memory_id,
uid,
)
return False
def sync_canonical_memory_vector(
item: MemoryItem,
*,
projection_commit_id: Optional[str] = None,
on_hard_failure: Optional[Callable[[], None]] = None,
db_client: Any = None,
) -> bool:
"""Converge one live canonical item without indexing restricted content."""
if set(item.sensitivity_labels).intersection(RESTRICTED_SENSITIVITY_LABELS):
deleted = delete_canonical_memory_vector(item.uid, item.memory_id)
if not deleted and on_hard_failure is not None:
on_hard_failure()
return deleted
if (
item.status != MemoryItemStatus.active
or item.processing_state != ProcessingState.processed
or item.source_state != SourceState.active
or (item.promotion or {}).get("user_review") is False
):
return False
content = (item.content or "").strip()
if not content:
return False
try:
from database.vector_db import upsert_canonical_memory_vector
# upsert_canonical_memory_vector carries its own external-write fence.
result = upsert_canonical_memory_vector(item, projection_commit_id=projection_commit_id)
except Exception:
logger.exception(
"canonical vector sync failed memory_id=%s uid=%s",
item.memory_id,
item.uid,
)
if on_hard_failure is not None:
on_hard_failure()
return False
if result is None:
logger.warning("canonical vector sync skipped memory_id=%s uid=%s", item.memory_id, item.uid)
return False
return True