forked from BasedHardware/omi
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmemory_vector_metadata.py
More file actions
285 lines (239 loc) · 10.5 KB
/
Copy pathmemory_vector_metadata.py
File metadata and controls
285 lines (239 loc) · 10.5 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
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
"""Canonical provider identity, vector metadata builders, and parsers (WS-G7)."""
from __future__ import annotations
import hashlib
from dataclasses import dataclass
from datetime import datetime
from typing import Any, Collection, Dict, Optional, cast
from models.memory_search_gateway import SearchDecision, SearchVectorHit
from models.knowledge_ledger_search import (
LEDGER_INDEX_VERSION,
LEDGER_SEARCH_KINDS,
build_ledger_index_metadata,
validate_ledger_kinds,
)
from models.product_memory import RESTRICTED_SENSITIVITY_LABELS, MemoryTier, MemoryItem
MEMORY_VECTOR_SCHEMA_VERSION = 1
CANONICAL_MEMORY_PROVIDER_ID_PREFIX = "memproj"
@dataclass(frozen=True)
class ParsedVectorHit:
hit: Optional[SearchVectorHit]
decision: SearchDecision
reason: str
@dataclass(frozen=True)
class ParsedMemoryVectorHit:
hit: Optional[SearchVectorHit]
decision: SearchDecision
reason: str
def canonical_memory_provider_id(uid: str, memory_id: str) -> str:
"""Return the sole external-provider identity for one user's canonical memory."""
if not uid.strip():
raise ValueError("uid is required")
if not memory_id.strip():
raise ValueError("memory_id is required")
payload = f"{uid}\0{memory_id}".encode("utf-8")
return f"{CANONICAL_MEMORY_PROVIDER_ID_PREFIX}:{hashlib.sha256(payload).hexdigest()}"
def build_canonical_memory_vector_delete_filter(
uid: str,
memory_id: Optional[str] = None,
) -> Dict[str, Any]:
"""Fence vector cleanup to one user and optional canonical memory.
The filter intentionally does not require the current schema marker:
account/privacy cleanup must also remove legacy and partially migrated rows.
"""
if not uid.strip():
raise ValueError("uid is required")
clauses: list[Dict[str, Any]] = [{"uid": {"$eq": uid}}]
if memory_id is not None:
if not memory_id.strip():
raise ValueError("memory_id is required")
clauses.append({"memory_id": {"$eq": memory_id}})
return {"$and": clauses}
def _shared_memory_vector_metadata_fields(
item: MemoryItem,
*,
projection_commit_id: str,
vector_updated_at: datetime,
) -> Dict[str, Any]:
if not projection_commit_id or not projection_commit_id.strip():
raise ValueError("projection_commit_id is required")
if vector_updated_at.tzinfo is None or vector_updated_at.utcoffset() is None:
raise ValueError("vector_updated_at must be timezone-aware")
labels = sorted({label.strip().lower() for label in item.sensitivity_labels if label and label.strip()})
shared = {
"uid": item.uid,
"memory_id": item.memory_id,
"status": item.status.value,
"processing_state": item.processing_state.value,
"source_state": item.source_state.value,
"visibility": item.visibility,
"sensitivity_labels": labels,
"restricted_sensitivity": bool(set(labels).intersection(RESTRICTED_SENSITIVITY_LABELS)),
"account_generation": item.account_generation,
"item_revision": item.item_revision,
"source_commit_id": item.source_commit_id,
"content_hash": item.content_hash,
"projection_commit_id": projection_commit_id,
"vector_updated_at": vector_updated_at.isoformat(),
}
device_ids = sorted({d for d in (item.capture_device_ids or []) if d})
if not device_ids and item.primary_capture_device:
device_ids = [item.primary_capture_device]
if device_ids:
shared["capture_device_ids"] = device_ids
return strip_null_metadata_values(shared)
def build_memory_vector_metadata(
item: MemoryItem,
*,
projection_commit_id: str,
vector_updated_at: datetime,
) -> Dict[str, Any]:
"""Neutral metadata for universal canonical vectors (``memory_layer``, ``memory_schema_version``)."""
shared = _shared_memory_vector_metadata_fields(
item, projection_commit_id=projection_commit_id, vector_updated_at=vector_updated_at
)
metadata = {
"memory_schema_version": MEMORY_VECTOR_SCHEMA_VERSION,
"memory_layer": item.tier.value,
**shared,
}
metadata.update(build_ledger_index_metadata(item))
return metadata
def strip_null_metadata_values(metadata: Dict[str, Any]) -> Dict[str, Any]:
"""Return Pinecone-safe metadata without null values."""
return {key: value for key, value in metadata.items() if value is not None}
def build_default_memory_vector_filter(uid: str) -> Dict[str, Any]:
return _base_memory_vector_filter(
uid, {"memory_layer": {"$in": [MemoryTier.short_term.value, MemoryTier.long_term.value]}}
)
def build_ledger_memory_vector_filter(uid: str, kinds: Collection[str] = LEDGER_SEARCH_KINDS) -> Dict[str, Any]:
"""Build a provider filter for open, versioned ledger rows only."""
parsed_kinds = validate_ledger_kinds(kinds)
result = build_default_memory_vector_filter(uid)
result["$and"].extend(
[
{"ledger_index_version": {"$eq": LEDGER_INDEX_VERSION}},
{"ledger_schema_version": {"$eq": "knowledge_ledger.v1"}},
{"ledger_row_state": {"$eq": "open"}},
{"ledger_kind": {"$in": sorted(parsed_kinds)}},
]
)
return result
def build_archive_memory_vector_filter(uid: str) -> Dict[str, Any]:
return _base_memory_vector_filter(uid, {"memory_layer": {"$eq": MemoryTier.archive.value}})
def parse_memory_search_vector_hit(match: Dict[str, Any]) -> ParsedMemoryVectorHit:
raw_metadata = match.get("metadata")
metadata: Dict[str, Any] = cast(Dict[str, Any], raw_metadata) if isinstance(raw_metadata, dict) else {}
try:
if metadata.get("memory_schema_version") != MEMORY_VECTOR_SCHEMA_VERSION:
raise ValueError("wrong_schema")
memory_id = _required_str(metadata, "memory_id")
projection_commit_id = _required_str(metadata, "projection_commit_id")
vector_updated_at = _parse_timestamp(_required_str(metadata, "vector_updated_at"))
score = float(match.get("score", 0.0))
hit = SearchVectorHit(
vector_id=_optional_match_id(match),
memory_id=memory_id,
score=score,
projection_commit_id=projection_commit_id,
vector_updated_at=vector_updated_at,
uid=_optional_str(metadata, "uid"),
account_generation=_optional_int(metadata, "account_generation"),
item_revision=_optional_int(metadata, "item_revision"),
source_commit_id=_optional_str(metadata, "source_commit_id"),
content_hash=_optional_str(metadata, "content_hash"),
)
except (TypeError, ValueError):
return ParsedMemoryVectorHit(
hit=None, decision=SearchDecision.stale_vector, reason="invalid_or_missing_vector_metadata"
)
return ParsedMemoryVectorHit(hit=hit, decision=SearchDecision.allowed, reason="parsed")
def parse_search_vector_hit(match: Dict[str, Any]) -> ParsedVectorHit:
raw_metadata = match.get("metadata")
metadata: Dict[str, Any] = cast(Dict[str, Any], raw_metadata) if isinstance(raw_metadata, dict) else {}
try:
if metadata.get("memory_schema_version") != MEMORY_VECTOR_SCHEMA_VERSION:
raise ValueError("wrong_schema")
memory_id = _required_str(metadata, "memory_id")
projection_commit_id = _required_str(metadata, "projection_commit_id")
vector_updated_at = _parse_timestamp(_required_str(metadata, "vector_updated_at"))
score = float(match.get("score", 0.0))
hit = SearchVectorHit(
vector_id=_optional_match_id(match),
memory_id=memory_id,
score=score,
projection_commit_id=projection_commit_id,
vector_updated_at=vector_updated_at,
uid=_optional_str(metadata, "uid"),
account_generation=_optional_int(metadata, "account_generation"),
item_revision=_optional_int(metadata, "item_revision"),
source_commit_id=_optional_str(metadata, "source_commit_id"),
content_hash=_optional_str(metadata, "content_hash"),
)
except (TypeError, ValueError):
return ParsedVectorHit(
hit=None, decision=SearchDecision.stale_vector, reason="invalid_or_missing_vector_metadata"
)
return ParsedVectorHit(hit=hit, decision=SearchDecision.allowed, reason="parsed")
def _active_memory_vector_filter_clauses() -> list[Dict[str, Any]]:
return [
{"status": {"$eq": "active"}},
{"source_state": {"$eq": "active"}},
{"visibility": {"$in": ["private", "public", "shared"]}},
{"restricted_sensitivity": {"$eq": False}},
]
def _base_memory_vector_filter(uid: str, layer_filter: Dict[str, Any]) -> Dict[str, Any]:
if not uid or not uid.strip():
raise ValueError("uid is required")
return {
"$and": [
{"uid": {"$eq": uid}},
{"memory_schema_version": {"$eq": MEMORY_VECTOR_SCHEMA_VERSION}},
layer_filter,
*_active_memory_vector_filter_clauses(),
]
}
def _optional_match_id(match: Dict[str, Any]) -> Optional[str]:
value = match.get("id")
if value is None:
return None
if not isinstance(value, str) or not value.strip():
raise ValueError("id")
return value
def _required_str(metadata: Dict[str, Any], key: str) -> str:
value = metadata.get(key)
if not isinstance(value, str) or not value.strip():
raise ValueError(key)
return value
def _optional_str(metadata: Dict[str, Any], key: str) -> Optional[str]:
value = metadata.get(key)
if value is None:
return None
if not isinstance(value, str) or not value.strip():
raise ValueError(key)
return value
def _optional_int(metadata: Dict[str, Any], key: str) -> Optional[int]:
value = metadata.get(key)
if value is None:
return None
return int(value)
def _parse_timestamp(value: str) -> datetime:
timestamp = datetime.fromisoformat(value.replace("Z", "+00:00"))
if timestamp.tzinfo is None or timestamp.utcoffset() is None:
raise ValueError("naive_timestamp")
return timestamp
__all__ = [
"CANONICAL_MEMORY_PROVIDER_ID_PREFIX",
"MEMORY_VECTOR_SCHEMA_VERSION",
"RESTRICTED_SENSITIVITY_LABELS",
"ParsedMemoryVectorHit",
"ParsedVectorHit",
"build_archive_memory_vector_filter",
"build_canonical_memory_vector_delete_filter",
"build_default_memory_vector_filter",
"build_ledger_memory_vector_filter",
"build_memory_vector_metadata",
"canonical_memory_provider_id",
"parse_memory_search_vector_hit",
"parse_search_vector_hit",
"strip_null_metadata_values",
]