|
1 | 1 | """Chat state models.""" |
2 | 2 |
|
3 | | -from datetime import UTC, datetime |
| 3 | +from datetime import UTC, datetime, timedelta |
4 | 4 |
|
5 | 5 | from pydantic import BaseModel, Field |
6 | 6 |
|
@@ -28,12 +28,32 @@ class ChatInfo(BaseModel): |
28 | 28 | title: str | None = None |
29 | 29 |
|
30 | 30 |
|
| 31 | +class MutationConfirmation(BaseModel): |
| 32 | + """Chat-scoped proof that a mutating operation was shown and confirmed.""" |
| 33 | + |
| 34 | + plan_id: str |
| 35 | + capability_id: str |
| 36 | + operation: str |
| 37 | + status: str = "presented" # presented | confirmed | executed |
| 38 | + presented_at: datetime = Field(default_factory=lambda: datetime.now(UTC)) |
| 39 | + expires_at: datetime |
| 40 | + confirmed_at: datetime | None = None |
| 41 | + executed_at: datetime | None = None |
| 42 | + target_fingerprint: str | None = None |
| 43 | + thread_id: str | None = None |
| 44 | + summary: str | None = None |
| 45 | + |
| 46 | + |
31 | 47 | class ChatState(BaseModel): |
32 | 48 | """State for a chat, stored in state.json.""" |
33 | 49 |
|
34 | 50 | chat: ChatInfo |
35 | 51 | participants: list[Participant] = Field(default_factory=list) |
36 | 52 | thread_index: dict[str, str] = Field(default_factory=dict) |
| 53 | + active_thread_id: str | None = None |
| 54 | + active_thread_updated_at: datetime | None = None |
| 55 | + active_thread_reason: str | None = None |
| 56 | + mutation_confirmations: list[MutationConfirmation] = Field(default_factory=list) |
37 | 57 | updated_at: datetime = Field(default_factory=lambda: datetime.now(UTC)) |
38 | 58 | graph_chat_id: str | None = None # Reference to graph ChatEntry.id |
39 | 59 |
|
@@ -73,3 +93,144 @@ def update_participant( |
73 | 93 |
|
74 | 94 | self.updated_at = now |
75 | 95 | return participant |
| 96 | + |
| 97 | + def set_active_thread( |
| 98 | + self, |
| 99 | + thread_id: str, |
| 100 | + *, |
| 101 | + reason: str, |
| 102 | + now: datetime | None = None, |
| 103 | + ) -> None: |
| 104 | + """Record the active thread for chat-scoped DM routing.""" |
| 105 | + ts = now or datetime.now(UTC) |
| 106 | + self.active_thread_id = str(thread_id) |
| 107 | + self.active_thread_updated_at = ts |
| 108 | + self.active_thread_reason = reason |
| 109 | + self.updated_at = ts |
| 110 | + |
| 111 | + def get_active_thread( |
| 112 | + self, |
| 113 | + *, |
| 114 | + max_age_minutes: int, |
| 115 | + now: datetime | None = None, |
| 116 | + ) -> str | None: |
| 117 | + """Return active thread_id if it is still within the freshness window.""" |
| 118 | + if not self.active_thread_id or not self.active_thread_updated_at: |
| 119 | + return None |
| 120 | + ts = now or datetime.now(UTC) |
| 121 | + max_age = max(1, int(max_age_minutes)) |
| 122 | + if ts - self.active_thread_updated_at > timedelta(minutes=max_age): |
| 123 | + return None |
| 124 | + return self.active_thread_id |
| 125 | + |
| 126 | + def add_mutation_confirmation( |
| 127 | + self, |
| 128 | + *, |
| 129 | + plan_id: str, |
| 130 | + capability_id: str, |
| 131 | + operation: str, |
| 132 | + target_fingerprint: str | None = None, |
| 133 | + thread_id: str | None = None, |
| 134 | + summary: str | None = None, |
| 135 | + ttl_hours: int = 24, |
| 136 | + now: datetime | None = None, |
| 137 | + ) -> MutationConfirmation: |
| 138 | + """Store a mutation confirmation prompt shown to the user.""" |
| 139 | + ts = now or datetime.now(UTC) |
| 140 | + self.prune_expired_mutation_confirmations(now=ts) |
| 141 | + confirmation = MutationConfirmation( |
| 142 | + plan_id=plan_id, |
| 143 | + capability_id=capability_id, |
| 144 | + operation=operation, |
| 145 | + expires_at=ts + timedelta(hours=max(1, int(ttl_hours))), |
| 146 | + target_fingerprint=target_fingerprint, |
| 147 | + thread_id=thread_id, |
| 148 | + summary=summary, |
| 149 | + ) |
| 150 | + self.mutation_confirmations.append(confirmation) |
| 151 | + self.updated_at = ts |
| 152 | + return confirmation |
| 153 | + |
| 154 | + def confirm_latest_mutation( |
| 155 | + self, |
| 156 | + *, |
| 157 | + now: datetime | None = None, |
| 158 | + thread_id: str | None = None, |
| 159 | + ) -> MutationConfirmation | None: |
| 160 | + """Confirm the latest non-expired presented mutation plan.""" |
| 161 | + ts = now or datetime.now(UTC) |
| 162 | + self.prune_expired_mutation_confirmations(now=ts) |
| 163 | + for confirmation in reversed(self.mutation_confirmations): |
| 164 | + if confirmation.status != "presented": |
| 165 | + continue |
| 166 | + if ( |
| 167 | + thread_id |
| 168 | + and confirmation.thread_id |
| 169 | + and confirmation.thread_id != thread_id |
| 170 | + ): |
| 171 | + continue |
| 172 | + confirmation.status = "confirmed" |
| 173 | + confirmation.confirmed_at = ts |
| 174 | + self.updated_at = ts |
| 175 | + return confirmation |
| 176 | + return None |
| 177 | + |
| 178 | + def find_confirmed_mutation( |
| 179 | + self, |
| 180 | + *, |
| 181 | + capability_id: str, |
| 182 | + operation: str, |
| 183 | + target_fingerprint: str | None = None, |
| 184 | + thread_id: str | None = None, |
| 185 | + now: datetime | None = None, |
| 186 | + ) -> MutationConfirmation | None: |
| 187 | + """Find a non-expired confirmed mutation authorization.""" |
| 188 | + ts = now or datetime.now(UTC) |
| 189 | + self.prune_expired_mutation_confirmations(now=ts) |
| 190 | + for confirmation in reversed(self.mutation_confirmations): |
| 191 | + if confirmation.status != "confirmed": |
| 192 | + continue |
| 193 | + if confirmation.capability_id != capability_id: |
| 194 | + continue |
| 195 | + if confirmation.operation != operation: |
| 196 | + continue |
| 197 | + if target_fingerprint and confirmation.target_fingerprint: |
| 198 | + if confirmation.target_fingerprint != target_fingerprint: |
| 199 | + continue |
| 200 | + if ( |
| 201 | + thread_id |
| 202 | + and confirmation.thread_id |
| 203 | + and confirmation.thread_id != thread_id |
| 204 | + ): |
| 205 | + continue |
| 206 | + return confirmation |
| 207 | + return None |
| 208 | + |
| 209 | + def mark_mutation_executed( |
| 210 | + self, |
| 211 | + *, |
| 212 | + plan_id: str, |
| 213 | + now: datetime | None = None, |
| 214 | + ) -> bool: |
| 215 | + """Mark a confirmed mutation plan as executed.""" |
| 216 | + ts = now or datetime.now(UTC) |
| 217 | + for confirmation in self.mutation_confirmations: |
| 218 | + if confirmation.plan_id != plan_id: |
| 219 | + continue |
| 220 | + confirmation.status = "executed" |
| 221 | + confirmation.executed_at = ts |
| 222 | + self.updated_at = ts |
| 223 | + return True |
| 224 | + return False |
| 225 | + |
| 226 | + def prune_expired_mutation_confirmations( |
| 227 | + self, |
| 228 | + *, |
| 229 | + now: datetime | None = None, |
| 230 | + ) -> None: |
| 231 | + """Remove expired mutation confirmation entries.""" |
| 232 | + ts = now or datetime.now(UTC) |
| 233 | + kept = [item for item in self.mutation_confirmations if item.expires_at > ts] |
| 234 | + if len(kept) != len(self.mutation_confirmations): |
| 235 | + self.mutation_confirmations = kept |
| 236 | + self.updated_at = ts |
0 commit comments