hezarfen_backend
Clone
https://git.tahinli.com/Hezarfen/hezarfen_backend.git
0 watching0 forksRust8.3 MiB6d68754Ahmet Kaan Gümüş2 months ago566 commits
| 1 | //! The `chatbot_message` table: the turns of a thread, both sides persisted. |
| 2 | //! The user's prompt lands `complete`, and the assistant's row is written |
| 3 | //! `pending` *before* the AI call so a reload never loses an answer in |
| 4 | //! flight. The task that owns the bridge round trip then flips it to |
| 5 | //! `complete` (with the text) or `failed` (with a code), and the boot sweep |
| 6 | //! in `database.rs` fails whatever a process death left behind past the |
| 7 | //! staleness window. Every turn is written *through* its thread's own row — |
| 8 | //! its guarded statement bumps `updated_at`, so a turn can never outlive |
| 9 | //! the thread it belongs to. The row shape lives in |
| 10 | //! [`crate::domain::chatbot_message`]. |
| 11 | |
| 12 | use crateMAX_ERROR_CODE_LEN; |
| 13 | use crateDatabase; |
| 14 | use cratePagedList; |
| 15 | use crate |
| 16 | |
| 17 | ; |
| 18 | use crateChatbotThreadId; |
| 19 | use crateTimestamp; |
| 20 | use crateUserId; |
| 21 | use crateAppError; |
| 22 | use query_as; |
| 23 | |
| 24 | /// Write one turn *through its thread's own row*: the thread's activity |
| 25 | /// bump and the turn's insert are a single guarded statement, which is what |
| 26 | /// makes the thread's existence something this write writes rather than |
| 27 | /// something the caller read and then trusted — a |
| 28 | /// [`delete`](crate::db::chatbot_thread::delete) racing it touches the very |
| 29 | /// row this statement's gate updates, so under Postgres's row locking the |
| 30 | /// two serialize and no turn is left under a thread that is gone. It also |
| 31 | /// carries the thread's activity stamp, so a turn and its `updated_at` land |
| 32 | /// together. The stamp is written strictly upwards — |
| 33 | /// `GREATEST($now, updated_at + 1)` — because a turn's two rows routinely |
| 34 | /// land inside one millisecond and `updated_at` is the thread list's |
| 35 | /// ordering key. |
| 36 | /// |
| 37 | /// [`AppError::NotFound`] = the thread is gone, and nothing was written. |
| 38 | async |
| 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 | /// Append the user's prompt. Nothing is awaited for it, so it is born |
| 74 | /// complete. |
| 75 | pub async |
| 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 | /// Reserve the assistant's answer *before* the AI call: the row exists, |
| 101 | /// empty and `pending`, so a reload finds the turn and can wait on it. |
| 102 | pub async |
| 103 | |
| 104 | |
| 105 | |
| 106 | |
| 107 | |
| 108 | |
| 109 | |
| 110 | |
| 111 | |
| 112 | |
| 113 | |
| 114 | |
| 115 | |
| 116 | |
| 117 | |
| 118 | |
| 119 | |
| 120 | |
| 121 | |
| 122 | |
| 123 | |
| 124 | |
| 125 | /// The whole thread, oldest first — the order both the UI and the AI |
| 126 | /// history payload read in. |
| 127 | pub async |
| 128 | |
| 129 | |
| 130 | |
| 131 | |
| 132 | |
| 133 | |
| 134 | |
| 135 | |
| 136 | |
| 137 | |
| 138 | |
| 139 | |
| 140 | |
| 141 | |
| 142 | |
| 143 | |
| 144 | |
| 145 | |
| 146 | /// The last `limit` turns, still oldest-first — the tail replayed to the |
| 147 | /// AI service as context. Taken newest-first in the database (so the |
| 148 | /// `LIMIT` keeps the *recent* end) and flipped back here. |
| 149 | pub async |
| 150 | |
| 151 | |
| 152 | |
| 153 | |
| 154 | |
| 155 | |
| 156 | |
| 157 | |
| 158 | |
| 159 | |
| 160 | |
| 161 | |
| 162 | |
| 163 | |
| 164 | |
| 165 | |
| 166 | |
| 167 | |
| 168 | |
| 169 | |
| 170 | |
| 171 | |
| 172 | /// The last `limit` *settled* turns that carry text, still oldest-first — |
| 173 | /// the tail replayed to the AI service as context. The filter runs in the |
| 174 | /// query, not after it: a `LIMIT` over the raw tail hands back fewer usable |
| 175 | /// rows the moment a run of answers fails, silently shrinking the context |
| 176 | /// instead of reaching further back. No projection is applied — it only |
| 177 | /// ever rewrites a `pending` row, and none is selected here. |
| 178 | pub async |
| 179 | |
| 180 | |
| 181 | |
| 182 | |
| 183 | |
| 184 | |
| 185 | |
| 186 | |
| 187 | |
| 188 | |
| 189 | |
| 190 | |
| 191 | |
| 192 | |
| 193 | |
| 194 | |
| 195 | |
| 196 | |
| 197 | |
| 198 | |
| 199 | |
| 200 | |
| 201 | |
| 202 | /// The user turn a reserved answer belongs to, by *identity*: the prompt |
| 203 | /// row of the very POST that reserved it, whose id the answering task |
| 204 | /// carries. |
| 205 | /// |
| 206 | /// Never derived from write order. Two POSTs on one thread interleave |
| 207 | /// across the two creates — the rows land `userA, userB, asstA, asstB` — |
| 208 | /// so "the newest user row written before this answer" resolves *both* |
| 209 | /// answers to prompt B, and prompt A is never answered. |
| 210 | pub async |
| 211 | |
| 212 | |
| 213 | |
| 214 | |
| 215 | |
| 216 | |
| 217 | |
| 218 | |
| 219 | |
| 220 | |
| 221 | |
| 222 | |
| 223 | |
| 224 | |
| 225 | |
| 226 | |
| 227 | |
| 228 | |
| 229 | /// Read one turn only if `user` owns it — the poll loop's read. |
| 230 | pub async |
| 231 | |
| 232 | |
| 233 | |
| 234 | |
| 235 | |
| 236 | |
| 237 | |
| 238 | |
| 239 | |
| 240 | |
| 241 | |
| 242 | |
| 243 | |
| 244 | |
| 245 | |
| 246 | |
| 247 | |
| 248 | |
| 249 | |
| 250 | |
| 251 | |
| 252 | /// Land the answer, recording whether it had to be clipped to fit the |
| 253 | /// school's cap. Field-scoped and gated on `status = 'pending'` in the |
| 254 | /// `WHERE`, so a late reply can't overwrite a row the boot sweep (or a |
| 255 | /// timeout) already failed, and two answers can't both apply. |
| 256 | pub async |
| 257 | |
| 258 | |
| 259 | |
| 260 | |
| 261 | |
| 262 | |
| 263 | |
| 264 | |
| 265 | /// Mark the answer failed with a short code (`unavailable`, the service's |
| 266 | /// own error code, …). Same pending gate as [`complete`]. |
| 267 | pub async |
| 268 | |
| 269 | |
| 270 | |
| 271 | |
| 272 | |
| 273 | |
| 274 | |
| 275 | |
| 276 | async |
| 277 | |
| 278 | |
| 279 | |
| 280 | |
| 281 | |
| 282 | |
| 283 | |
| 284 | |
| 285 | |
| 286 | |
| 287 | |
| 288 | |
| 289 | |
| 290 | |
| 291 | |
| 292 | |
| 293 | |
| 294 | |
| 295 | |
| 296 | |
| 297 | |
| 298 | |
| 299 | |
| 300 | |
| 301 | |
| 302 | |
| 303 | |
| 304 | |
| 305 | |
| 306 | |
| 307 | |
| 308 | |
| 309 | |
| 310 | |
| 311 | |
| 312 | |
| 313 | |
| 314 | |
| 315 | |
| 316 | |
| 317 | |
| 318 | |
| 319 | |
| 320 | |
| 321 | |
| 322 | |
| 323 | |
| 324 | |
| 325 | |
| 326 | |
| 327 | |
| 328 | |
| 329 | |
| 330 | |
| 331 | |
| 332 | |
| 333 | |
| 334 | |
| 335 | |
| 336 | |
| 337 | |
| 338 | |
| 339 | |
| 340 | |
| 341 | |
| 342 | |
| 343 | |
| 344 | |
| 345 | |
| 346 | |
| 347 | |
| 348 | |
| 349 | |
| 350 | |
| 351 | |
| 352 | |
| 353 | |
| 354 | |
| 355 | |
| 356 | |
| 357 | |
| 358 | |
| 359 | |
| 360 | |
| 361 | |
| 362 |