forked from BasedHardware/omi
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathframe_request.py
More file actions
199 lines (162 loc) · 7.57 KB
/
Copy pathframe_request.py
File metadata and controls
199 lines (162 loc) · 7.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
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
"""Wire contracts for the additive just-in-time screen-frame request queue.
The queue carries metadata only. Pixels are uploaded by the owning desktop
device through the existing screen-sync path and are never accepted in this
API model or emitted as telemetry.
"""
from __future__ import annotations
from datetime import datetime, timezone
from enum import Enum
from typing import Any
from pydantic import BaseModel, ConfigDict, Field, field_validator, model_validator
class FrameRequestState(str, Enum):
requested = "requested"
claimed = "claimed"
uploaded = "uploaded"
attached = "attached"
offline = "offline"
pruned = "pruned"
failed = "failed"
expired = "expired"
cancelled = "cancelled"
class FrameRequestCleanupState(str, Enum):
"""External-pixel deletion state, independent of lifecycle terminality."""
not_required = "not_required"
pending = "pending"
failed = "failed"
deleted = "deleted"
permanent = "permanent"
TERMINAL_FRAME_REQUEST_STATES = frozenset(
{
FrameRequestState.attached,
FrameRequestState.offline,
FrameRequestState.pruned,
FrameRequestState.failed,
FrameRequestState.expired,
FrameRequestState.cancelled,
}
)
class FrameRequest(BaseModel):
"""Owner-scoped request and its auditable lifecycle state."""
model_config = ConfigDict(extra="forbid")
request_id: str = Field(min_length=1, max_length=128)
uid: str = Field(min_length=1, max_length=256)
device_id: str = Field(min_length=1, max_length=256)
account_generation: int = Field(default=0, ge=0)
dedupe_key: str = Field(min_length=1, max_length=256)
# The identity is reusable after a terminal/expired attempt. These fields
# make that boundary explicit instead of letting a forever-stable document
# id starve future requests.
dedupe_window: int = Field(default=0, ge=0)
attempt_number: int = Field(default=0, ge=0)
conversation_id: str | None = Field(default=None, max_length=256)
screenshot_id: str | None = Field(default=None, max_length=256)
state: FrameRequestState = FrameRequestState.requested
created_at: datetime
expires_at: datetime
claimed_at: datetime | None = None
uploaded_at: datetime | None = None
attached_at: datetime | None = None
terminal_reason: str | None = Field(default=None, max_length=240)
byte_count: int = Field(default=0, ge=0, le=10 * 1024 * 1024)
content_type: str | None = Field(default=None, max_length=100)
storage_id: str | None = Field(default=None, max_length=256)
cleanup_state: FrameRequestCleanupState = FrameRequestCleanupState.not_required
cleanup_attempts: int = Field(default=0, ge=0, le=1000)
cleanup_next_attempt_at: datetime | None = None
@field_validator("uid", "device_id", "request_id", "dedupe_key", mode="before")
@classmethod
def _strip_required_strings(cls, value: Any) -> str:
if not isinstance(value, str):
raise TypeError("frame request identifiers must be strings")
value = value.strip()
if not value:
raise ValueError("frame request identifiers must not be blank")
return value
@field_validator("request_id")
@classmethod
def _validate_request_id(cls, value: str) -> str:
if "/" in value or "\\" in value:
raise ValueError("request_id must be one opaque path segment")
return value
@field_validator("conversation_id", "screenshot_id", "terminal_reason", "content_type", "storage_id", mode="before")
@classmethod
def _strip_optional_strings(cls, value: Any) -> str | None:
if value is None:
return None
if not isinstance(value, str):
raise TypeError("frame request optional fields must be strings")
value = value.strip()
return value or None
@field_validator("storage_id")
@classmethod
def _validate_storage_id(cls, value: str | None) -> str | None:
if value is None:
return None
if "/" in value or "\\" in value or value.startswith(("http:", "https:")):
raise ValueError("storage_id must be an opaque owner-scoped identifier")
return value
@field_validator("created_at", "expires_at", "claimed_at", "uploaded_at", "attached_at", "cleanup_next_attempt_at")
@classmethod
def _normalize_datetime(cls, value: datetime | None) -> datetime | None:
if value is None:
return None
if value.tzinfo is None:
return value.replace(tzinfo=timezone.utc)
return value.astimezone(timezone.utc)
@model_validator(mode="after")
def _validate_lifecycle(self) -> FrameRequest:
if self.expires_at < self.created_at:
raise ValueError("frame request expiry must not precede creation")
if self.state == FrameRequestState.attached and not self.conversation_id:
raise ValueError("attached frame requests require a conversation")
if self.state == FrameRequestState.attached and self.expires_at != self.created_at:
raise ValueError("attached frame requests must not carry a time-based expiry")
if self.state == FrameRequestState.attached and self.terminal_reason:
raise ValueError("attached frame requests do not carry a terminal reason")
if (
self.state in TERMINAL_FRAME_REQUEST_STATES
and self.state != FrameRequestState.attached
and not self.terminal_reason
):
raise ValueError("terminal frame requests require a bounded reason")
if self.state == FrameRequestState.uploaded and not self.storage_id:
raise ValueError("uploaded frame requests require a storage id")
return self
class CreateFrameRequest(BaseModel):
model_config = ConfigDict(extra="forbid")
device_id: str = Field(min_length=1, max_length=256)
account_generation: int = Field(default=0, ge=0)
dedupe_key: str = Field(min_length=1, max_length=256)
conversation_id: str | None = Field(default=None, max_length=256)
screenshot_id: str | None = Field(default=None, max_length=256)
requested_ttl_seconds: int | None = Field(default=None, ge=1, le=6 * 24 * 60 * 60)
class FrameRequestStateUpdate(BaseModel):
model_config = ConfigDict(extra="forbid")
state: FrameRequestState
device_id: str = Field(min_length=1, max_length=256)
account_generation: int = Field(default=0, ge=0)
terminal_reason: str | None = Field(default=None, max_length=240)
storage_id: str | None = Field(default=None, max_length=256)
byte_count: int = Field(default=0, ge=0, le=10 * 1024 * 1024)
content_type: str | None = Field(default=None, max_length=100)
@field_validator("storage_id")
@classmethod
def _validate_storage_id(cls, value: str | None) -> str | None:
if value is None:
return None
value = value.strip()
if "/" in value or "\\" in value or value.startswith(("http:", "https:")):
raise ValueError("storage_id must be an opaque owner-scoped identifier")
return value or None
class FrameRequestPromotion(BaseModel):
model_config = ConfigDict(extra="forbid")
device_id: str = Field(min_length=1, max_length=256)
account_generation: int = Field(default=0, ge=0)
conversation_id: str = Field(min_length=1, max_length=256)
class FrameRequestEnvelope(BaseModel):
model_config = ConfigDict(extra="forbid")
request: FrameRequest
deduplicated: bool = False
class FrameRequestBatch(BaseModel):
model_config = ConfigDict(extra="forbid")
requests: list[FrameRequest] = Field(default_factory=list, max_length=32)