forked from BasedHardware/omi
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathprojections.py
More file actions
99 lines (82 loc) · 3.57 KB
/
Copy pathprojections.py
File metadata and controls
99 lines (82 loc) · 3.57 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
"""Canonical projections module (WS-G8a).
Neutral ``projections`` is the source of truth. Canonical projections module.
"""
import copy
import hashlib
from typing import Any, Dict, List
def _status(fact: Dict[str, Any]) -> str:
return fact.get("status") or fact.get("memory_state") or "active"
def _content_hash(content: str) -> str:
return hashlib.sha256((content or "").encode("utf-8")).hexdigest()
def _entity_id(value: Any) -> str:
return str(value) if value is not None else "unknown"
def _graph_projection(active_facts: List[Dict[str, Any]]) -> Dict[str, Any]:
nodes: Dict[str, Dict[str, Any]] = {}
edges: List[Dict[str, Any]] = []
for fact in active_facts:
subject = _entity_id(fact.get("subject_entity_id") or fact.get("subject") or "user")
nodes.setdefault(subject, {"entity_id": subject, "source": "durable_fact_projection"})
arguments = copy.deepcopy(fact.get("arguments") or {})
for key, value in arguments.items():
if key.endswith("_entity_id") or key in {"object_entity_id", "person_entity_id", "organization_entity_id"}:
object_id = _entity_id(value)
nodes.setdefault(object_id, {"entity_id": object_id, "source": "durable_fact_projection"})
edges.append(
{
"edge_id": f"edge_{fact.get('id')}",
"memory_id": fact.get("id"),
"subject_entity_id": subject,
"predicate": fact.get("predicate"),
"arguments": arguments,
"qualifiers": copy.deepcopy(fact.get("qualifiers") or {}),
}
)
return {"nodes": nodes, "edges": edges}
def rebuild_memory_memory_projections(facts: Dict[str, Dict[str, Any]] | List[Dict[str, Any]]) -> Dict[str, Any]:
if isinstance(facts, dict):
fact_rows = [copy.deepcopy(value) for _, value in sorted(facts.items(), key=lambda item: item[0])]
else:
fact_rows = sorted([copy.deepcopy(row) for row in facts], key=lambda row: row.get("id") or "")
active_facts = [fact for fact in fact_rows if _status(fact) == "active"]
review_facts = [fact for fact in fact_rows if _status(fact) == "review"]
context_facts = [fact for fact in fact_rows if _status(fact) == "context_only"]
vector_index = [
{
"memory_id": fact.get("id"),
"content": fact.get("content"),
"content_hash": _content_hash(fact.get("content") or ""),
"predicate": fact.get("predicate"),
"status": "active",
"projection_source": "durable_ledger_fact",
}
for fact in active_facts
]
review_queue = [
{
"memory_id": fact.get("id"),
"content": fact.get("content"),
"status": _status(fact),
"evidence_count": len(fact.get("evidence_set") or []),
"projection_source": "durable_ledger_fact",
}
for fact in review_facts
]
context_index = [
{
"memory_id": fact.get("id"),
"content": fact.get("content"),
"status": _status(fact),
"projection_source": "durable_ledger_fact",
}
for fact in context_facts
]
return {
"schema_version": "memory_projections.v1",
"vector_index": vector_index,
"graph": _graph_projection(active_facts),
"review_queue": review_queue,
"context_index": context_index,
"source_of_truth": "memory_ledger",
}
# Neutral symbol aliases (memory names remain valid via shim)
rebuild_memory_projections = rebuild_memory_memory_projections