forked from BasedHardware/omi
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathshort_term_memories.py
More file actions
78 lines (67 loc) · 3.1 KB
/
Copy pathshort_term_memories.py
File metadata and controls
78 lines (67 loc) · 3.1 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
76
77
78
"""Legacy shadow short_term collection helpers (data drain only).
The universal runtime uses memory_items; these helpers remain for historical shadow rows
on cascade conversation delete (tombstone_source) and review-queue resolve (mark_consolidated).
"""
from datetime import datetime, timezone
from typing import Any, Dict, List, Optional, TypeGuard, cast
from ._client import db
users_collection = 'users'
short_term_collection = 'short_term'
Payload = Dict[str, Any]
def _is_payload(value: object) -> TypeGuard[Payload]:
return isinstance(value, dict)
def mark_consolidated(uid: str, short_term_id: str, commit_id: Optional[str]) -> None:
doc_ref = db.collection(users_collection).document(uid).collection(short_term_collection).document(short_term_id)
# A conflict's source_short_term_id can point at an absent short-term doc (the universal runtime writes
# memory_items, not short_term). Firestore .update() raises NotFound on a missing doc (unlike set),
# which would surface as a 500 on resolve; no-op instead, mirroring memory_app_key_grants.
if not doc_ref.get().exists:
return
now = datetime.now(timezone.utc)
doc_ref.update(
{
'status': 'consolidated',
'consolidated_at': now,
'consolidated_commit_id': commit_id,
'soft_pruned_at': now,
'updated_at': now,
}
)
def tombstone_source(uid: str, source_id: str) -> List[str]:
collection_ref = db.collection(users_collection).document(uid).collection(short_term_collection)
now = datetime.now(timezone.utc)
tombstoned_ids: List[str] = []
for doc in collection_ref.stream():
memory = cast(Payload, doc.to_dict() or {})
raw_evidence: object = memory.get('evidence') or []
evidence: List[object] = cast(List[object], raw_evidence) if isinstance(raw_evidence, list) else []
if not any(_is_payload(item) and item.get('source_id') == source_id for item in evidence):
continue
tombstoned_evidence: List[object] = []
for item in evidence:
if not _is_payload(item):
tombstoned_evidence.append(item)
continue
next_item: Payload = dict(item)
if next_item.get('source_id') == source_id:
next_item['redaction_status'] = 'tombstoned'
next_item['tombstoned_at'] = now
tombstoned_evidence.append(next_item)
active_evidence: List[Payload] = [
item
for item in tombstoned_evidence
if _is_payload(item) and item.get('redaction_status', 'active') != 'tombstoned'
]
update_payload: Payload = {'evidence': tombstoned_evidence, 'updated_at': now}
if not active_evidence:
update_payload.update(
{
'status': 'source_tombstoned',
'soft_pruned_at': now,
'content': None,
'redaction_status': 'payload_tombstoned',
}
)
doc.reference.update(update_payload)
tombstoned_ids.append(doc.id)
return tombstoned_ids