forked from BasedHardware/omi
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmemory_imports.py
More file actions
189 lines (162 loc) · 6.08 KB
/
Copy pathmemory_imports.py
File metadata and controls
189 lines (162 loc) · 6.08 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
from __future__ import annotations
import hashlib
import os
import re
from dataclasses import dataclass
from datetime import datetime
from typing import Any, Optional
from google.api_core.exceptions import AlreadyExists, Conflict
from google.cloud import firestore
from database._client import document_id_from_seed
from database.memory_collections import MemoryCollections
from models.memory_imports import (
MemoryImportArtifact,
MemoryImportArtifactSourceState,
MemoryImportBatchItem,
MemoryImportBatchRequest,
MemoryImportBatchResponse,
MemoryImportRunStatus,
utc_now,
)
MEMORY_IMPORT_BODY_STORAGE_MODE_ENV = "MEMORY_IMPORT_BODY_STORAGE_MODE"
@dataclass(frozen=True)
class MemoryImportIngestResult:
response: MemoryImportBatchResponse
def _firestore_increment(value: int) -> Any:
return firestore.Increment(value)
def _normalized_source_type(source_type: str) -> str:
return "_".join((source_type or "").strip().lower().replace("-", "_").split())
def _stable_content_hash(item: MemoryImportBatchItem) -> str:
if item.content_hash:
return item.content_hash
hasher = hashlib.sha256()
for value in [item.title, item.snippet, item.content]:
if value:
hasher.update(value.encode("utf-8"))
hasher.update(b"\0")
return hasher.hexdigest()
def _body_storage_mode() -> str:
mode = (os.getenv(MEMORY_IMPORT_BODY_STORAGE_MODE_ENV) or "summary").strip().lower()
return mode if mode in {"summary", "full"} else "summary"
def _safe_client_document_id(uid: str, value: str) -> str:
if re.fullmatch(r"[A-Za-z0-9][A-Za-z0-9_.:-]{0,149}", value):
return value
return document_id_from_seed(f"memory-import-client-document-id|{uid}|{value}")
def _artifact_id(uid: str, source_type: str, source_account_hash: Optional[str], item: MemoryImportBatchItem) -> str:
content_hash = _stable_content_hash(item)
identity = "|".join(
[
source_account_hash or "",
item.external_id or "",
content_hash,
]
)
return document_id_from_seed(f"memory-import-artifact|{uid}|{source_type}|{identity}")
def _run_id(uid: str, request: MemoryImportBatchRequest) -> str:
if request.import_run_id:
return _safe_client_document_id(uid, request.import_run_id)
seed = "|".join(
[
"memory-import-run",
uid,
_normalized_source_type(request.source_type),
request.source_account_hash or "",
request.importer_version,
]
)
return document_id_from_seed(seed)
def ingest_memory_import_batch(
uid: str,
request: MemoryImportBatchRequest,
*,
db_client: Any,
now: Optional[datetime] = None,
) -> MemoryImportIngestResult:
"""Persist import artifacts only; never create product memories or indexes."""
current_time: datetime = now or utc_now()
source_type = _normalized_source_type(request.source_type)
run_id = _run_id(uid, request)
collections = MemoryCollections(uid=uid)
store_full_body = _body_storage_mode() == "full"
created = 0
deduped = 0
for item in request.items:
content_hash = _stable_content_hash(item)
artifact_id = _artifact_id(uid, source_type, request.source_account_hash, item)
artifact_ref: Any = db_client.document(f"{collections.memory_import_artifacts}/{artifact_id}")
artifact = MemoryImportArtifact(
artifact_id=artifact_id,
uid=uid,
run_id=run_id,
source_type=source_type,
external_id=item.external_id,
content_hash=content_hash,
title=item.title,
snippet=item.snippet,
redacted_body=item.content if store_full_body else None,
metadata=dict(item.metadata or {}),
occurred_at=item.occurred_at,
captured_at=current_time,
client_device_id=item.client_device_id,
redaction_status="importer_full_excerpt" if store_full_body else "title_snippet_only",
created_at=current_time,
updated_at=current_time,
)
try:
artifact_ref.create(artifact.model_dump(mode="json"))
created += 1
continue
except (AlreadyExists, Conflict):
deduped += 1
artifact_ref.set(
{
"run_id": run_id,
"source_state": MemoryImportArtifactSourceState.active.value,
"updated_at": current_time.isoformat(),
},
merge=True,
)
continue
run_ref: Any = db_client.document(f"{collections.memory_import_runs}/{run_id}")
try:
run_ref.create(
{
"run_id": run_id,
"uid": uid,
"source_type": source_type,
"source_account_hash": request.source_account_hash,
"importer_version": request.importer_version,
"extractor_version": request.extractor_version,
"status": MemoryImportRunStatus.received.value,
"artifact_count": 0,
"deduped_count": 0,
"candidate_count": 0,
"accepted_count": 0,
"promoted_count": 0,
"started_at": current_time.isoformat(),
"updated_at": current_time.isoformat(),
"completed_at": None,
"last_error": None,
}
)
except (AlreadyExists, Conflict):
pass
run_ref.set(
{
"artifact_count": _firestore_increment(created),
"deduped_count": _firestore_increment(deduped),
"updated_at": current_time.isoformat(),
},
merge=True,
)
return MemoryImportIngestResult(
response=MemoryImportBatchResponse(
run_id=run_id,
artifacts_received=len(request.items),
artifacts_created=created,
artifacts_deduped=deduped,
candidates_created=0,
status=MemoryImportRunStatus.received,
)
)
__all__ = ["ingest_memory_import_batch"]