forked from BasedHardware/omi
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmemory_api_contract.py
More file actions
75 lines (59 loc) · 2.48 KB
/
Copy pathmemory_api_contract.py
File metadata and controls
75 lines (59 loc) · 2.48 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
"""Authoritative memory API field contract.
Canonical lifecycle fields (`memory_tier` plus serialized `layer`) are only
product-visible for users whose request is routed through canonical memory.
Legacy users must stay untiered at API boundaries so desktop/mobile clients do
not infer Short-term/Long-term rollout state from internal defaults.
"""
from enum import Enum
from typing import Any, Dict, Iterable
from pydantic import BaseModel
from utils.memory.belief_model import belief_model_enabled
class MemoryApiExposure(str, Enum):
LEGACY = "legacy"
CANONICAL = "canonical"
CANONICAL_LIFECYCLE_FIELDS = frozenset({"memory_tier", "layer", "tier", "expires_at"})
BELIEF_VIEW_FIELDS = frozenset({"currency", "currency_band", "as_of", "half_life_days", "belief_class"})
MEMORY_INTERNAL_FIELDS = frozenset(
{
"memory_only",
"memory_default_memory",
"memory_source",
"memory_policy",
"source",
"policy",
"cursor",
"read_source",
"read_decision",
"source_policy",
"archive_default_available",
"archive_default_visible",
"stale_short_term_default_visible",
}
)
def _payload(value: BaseModel | Dict[str, Any]) -> Dict[str, Any]:
if isinstance(value, BaseModel):
return value.model_dump()
return dict(value)
def memory_api_payload(value: BaseModel | Dict[str, Any], exposure: MemoryApiExposure) -> Dict[str, Any]:
"""Serialize one memory for the requested API exposure."""
payload = _payload(value)
for field in MEMORY_INTERNAL_FIELDS:
payload.pop(field, None)
if exposure == MemoryApiExposure.LEGACY:
for field in CANONICAL_LIFECYCLE_FIELDS:
payload.pop(field, None)
elif exposure == MemoryApiExposure.CANONICAL:
tier = payload.get("memory_tier") or payload.get("tier")
if tier is not None and payload.get("layer") is None:
payload["layer"] = tier
if not belief_model_enabled():
for field in BELIEF_VIEW_FIELDS:
payload.pop(field, None)
return payload
def memory_api_payloads(
values: Iterable[BaseModel | Dict[str, Any]], exposure: MemoryApiExposure
) -> list[Dict[str, Any]]:
return [memory_api_payload(value, exposure) for value in values]
def memory_write_payload(value: BaseModel | Dict[str, Any], exposure: MemoryApiExposure) -> Dict[str, Any]:
"""Serialize one memory for persistence through the selected memory system."""
return memory_api_payload(value, exposure)