hezarfen_backend
Clone
https://git.tahinli.com/archive/hezarfen_backend.git
0 watching0 forksRust8.3 MiB6d68754Ahmet Kaan Gümüş2 months ago566 commits
| 1 | //! Payload contract for the `rag.index` capability, and the dispatch behind |
| 2 | //! it. |
| 3 | //! |
| 4 | //! Unlike [`chat`](crate::ai::chat) — a user waiting on an answer — this one |
| 5 | //! is a background refresh: a course note changed, so whatever a service had |
| 6 | //! indexed for it is stale. [`spawn_index`] therefore returns immediately and |
| 7 | //! the round trip runs in its own task, because no course-note handler may |
| 8 | //! slow down, fail, or 503 on account of an AI service. |
| 9 | //! |
| 10 | //! Failure is silence by design: no worker, a timeout, a transport error or an |
| 11 | //! unreadable answer all leave the previously stored |
| 12 | //! [`RagOutput`](crate::domain::rag_output::RagOutput) rows exactly as they |
| 13 | //! are. A slightly stale index beats no index. |
| 14 | //! |
| 15 | //! A service restart wipes an in-memory index and does not replay it. |
| 16 | //! [`spawn_index_replay`] runs when a worker that offers `rag.index` |
| 17 | //! registers, and calls [`index_course_note`] for every course note that has |
| 18 | //! files, in every active school. The spawn returns immediately: the QUIC |
| 19 | //! handshake must not wait on it. A chatbot, zeka, or podcast registration |
| 20 | //! does not offer `rag.index`, so it does not replay. |
| 21 | |
| 22 | use ; |
| 23 | |
| 24 | use crateAI_RAG_INDEX_TIMEOUT_SECS; |
| 25 | use crateDatabase; |
| 26 | use crateCourseNote; |
| 27 | use crateCourseNoteFile; |
| 28 | use crateModule; |
| 29 | use crateAppState; |
| 30 | use crateResolvedTenant; |
| 31 | |
| 32 | /// The capability string routed to an indexing service. Defined once, in |
| 33 | /// [`crate::constant`]; re-exported here so a reader of the payload contract |
| 34 | /// finds it next to the payloads. |
| 35 | pub use crateAI_RAG_INDEX_CAPABILITY; |
| 36 | |
| 37 | /// One attachment of the note, metadata only. |
| 38 | |
| 39 | |
| 40 | |
| 41 | |
| 42 | |
| 43 | |
| 44 | |
| 45 | |
| 46 | |
| 47 | |
| 48 | /// What the backend asks an indexing service to index. |
| 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 | /// Re-index `note` and replace its stored outputs with what the service |
| 89 | /// answered. Silent no-op when the bridge is off or no worker offers |
| 90 | /// `rag.index`. |
| 91 | /// |
| 92 | /// `tenant` is the caller's own school: its uuid rides the `hab/2` frame, and |
| 93 | /// `state.db` — the same school's database, since the caller reached this |
| 94 | /// through the shadow `State` — is where the answer is stored. |
| 95 | /// |
| 96 | /// A school that did not buy the `ai` package sends **nothing** to an AI |
| 97 | /// service: no dispatch, no `rag_output` row. That is `Module::Chatbot`, the |
| 98 | /// package's only module, so it gates every outbound dispatch and not just the |
| 99 | /// `/chatbot` nest. |
| 100 | /// |
| 101 | /// The answer may also name the doc id the service minted per file — `files` |
| 102 | /// as `[{id, doc_id}]` — which is stamped onto the matching attachment rows, |
| 103 | /// and only onto this note's own files. Best-effort: a failed stamp is logged |
| 104 | /// and the output still lands, because the map is optional on the wire and a |
| 105 | /// service that omits it is simply not heard from on that front. |
| 106 | pub async |
| 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 | |
| 200 | |
| 201 | |
| 202 | |
| 203 | |
| 204 | /// Swap the note's outputs for the fresh one. Create-then-drop-older, not an |
| 205 | /// update: the previous output belongs to a previous version of the note, and |
| 206 | /// this order is what makes two concurrent index tasks converge on one row |
| 207 | /// ([`replace_for_note`](crate::db::rag_output::replace_for_note)). |
| 208 | async |
| 209 | |
| 210 | |
| 211 | |
| 212 | |
| 213 | |
| 214 | |
| 215 | |
| 216 | |
| 217 | |
| 218 | |
| 219 | /// [`index_course_note`] off the request path: the handler has already |
| 220 | /// answered by the time the service is asked. |
| 221 | |
| 222 | |
| 223 | |
| 224 | |
| 225 | |
| 226 | |
| 227 | |
| 228 | /// Whether a worker that just registered should have the course-note index |
| 229 | /// replayed. Only `rag.index` does. `rag.chat`, the chatbot, zeka, and the |
| 230 | /// podcast worker do not — their registration must not walk every school. |
| 231 | pub |
| 232 | |
| 233 | |
| 234 | |
| 235 | |
| 236 | |
| 237 | /// [`replay_indexed_notes`] off the handshake path. Returns as soon as the |
| 238 | /// task is spawned. `state.ai` must already be the bridge that just |
| 239 | /// registered the worker; `state.db` may still be the control database — |
| 240 | /// each school is swapped in before [`index_course_note`]. |
| 241 | pub |
| 242 | |
| 243 | |
| 244 | |
| 245 | |
| 246 | |
| 247 | /// Re-dispatch [`index_course_note`] for every course note that has files, in |
| 248 | /// every active school. One note at a time: a replay is a catch-up, not a |
| 249 | /// stampede, and the worker's own concurrency cap is what bounds a single |
| 250 | /// dispatch. |
| 251 | async |
| 252 | |
| 253 | |
| 254 | |
| 255 | |
| 256 | |
| 257 | |
| 258 | |
| 259 | |
| 260 | |
| 261 | |
| 262 | |
| 263 | |
| 264 | async |
| 265 | |
| 266 | |
| 267 | |
| 268 | |
| 269 | |
| 270 | |
| 271 | |
| 272 | |
| 273 | |
| 274 | |
| 275 | |
| 276 | |
| 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 | |
| 363 | |
| 364 | |
| 365 | |
| 366 | |
| 367 | |
| 368 | |
| 369 | |
| 370 | |
| 371 | |
| 372 | |
| 373 | |
| 374 | |
| 375 | |
| 376 | |
| 377 | |
| 378 | |
| 379 | |
| 380 | |
| 381 | |
| 382 | |
| 383 | |
| 384 |